您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Babylon使用麥克風并處理常見問題的方法是什么”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Babylon使用麥克風并處理常見問題的方法是什么”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
在比較新的瀏覽器中,navigator對象中的getUserMedia方法已經被廢棄了。取而代之的是navigator.mediaDevices.getUserMedia(),并且constraints應該作為參數傳入。
const constraints = { audio: true, video: false }; navigator.mediaDevices.getUserMedia(constraints)
對于問題 Property 'constraints' does not exist on type 'Window & typeof globalThis'.ts(2339)
。這是因為 TypeScript 沒有識別到constraints變量的類型,可以嘗試直接定義一個 const constraints 常量。
如果在使用媒體設備流時,你遇到了“Property 'stream' does not exist on type 'Window & typeof globalThis'.ts(2339)” 這個問題出現在TypeScript項目中。因為該類型Script不認識window.stream, 所以我們需要將它聲明為any類型。解決方法如下:
(window as any).stream = stream;
這讓編譯器知道 window 對象現在具有一個名為stream的屬性。
在較新版本的Web API中,MediaStream
對象不再包含oninactive
屬性,需要更改事件監聽方式。 解決方法如下:
stream.addEventListener('inactive', () => { console.log('Stream ended ... ...'); });
而不是
stream.oninactive = () => { console.log('Stream ended ... ...'); };
此更改暴露了更多事件,同時也需要更改您的代碼來處理它們。
在 Babylon.js 中,使用媒體設備流時出現的問題通常是由于您沒有根據最新的約定執行操作,或者由于 TypeScript 編譯器不知道您所做的更改而導致的。通過遵循我們提供的指南和解決方案,您應該能夠輕松地解決這些問題。
示例源碼(修改前):
loadingASoundFromTheMicrophone = (scene: Scene, canvas: HTMLCanvasElement) => { let engine = scene.getEngine(); let freeCamera = new FreeCamera("freeCamera", new Vector3(0, 5, -10), scene); freeCamera.setTarget(Vector3.Zero()); freeCamera.attachControl(canvas, true); let hemisphericLight = new HemisphericLight("hemisphericLight", new Vector3(0, 1, 0), scene); hemisphericLight.intensity = 0.7; let sphere = MeshBuilder.CreateSphere("sphere", {segments: 16, diameter: 2}, scene); const constraints = window.constraints = { audio: true, video: false }; function handleSuccess(stream: MediaStream) { const audioTracks = stream.getAudioTracks(); console.log('Got stream with constraints:', constraints); console.log(`Using audio device: ${audioTracks[0].label}`); stream.oninactive = function() { console.log('Stream ended'); }; window.stream = stream; // make variable available to browser console var bjsSound = new Sound("mic", stream, scene); bjsSound.attachToMesh(sphere); bjsSound.play(); } function handleError(error: any) { console.log('navigator.getUserMedia error: ', error); } navigator.mediaDevices.getUserMedia(constraints).then(handleSuccess).catch(handleError); return scene; }
示例源碼(修改后):
loadingASoundFromTheMicrophone = (scene: Scene, canvas: HTMLCanvasElement) => { let engine = scene.getEngine(); let freeCamera = new FreeCamera("freeCamera", new Vector3(0, 5, -10), scene); freeCamera.setTarget(Vector3.Zero()); freeCamera.attachControl(canvas, true); let hemisphericLight = new HemisphericLight("hemisphericLight", new Vector3(0, 1, 0), scene); hemisphericLight.intensity = 0.7; let sphere = MeshBuilder.CreateSphere("sphere", {segments: 16, diameter: 2}, scene); const constraints = { audio: true, video: false }; function handleSuccess(stream: MediaStream) { const audioTracks = stream.getAudioTracks(); console.log('Got stream with constraints:', constraints); console.log(`Using audio device: ${audioTracks[0].label}`); stream.addEventListener('inactive', () => { console.log('Stream ended ... ...'); }); (window as any).stream = stream; // make variable available to browser console var bjsSound = new Sound("mic", stream, scene); bjsSound.attachToMesh(sphere); bjsSound.play(); } function handleError(error: any) { console.log('navigator.getUserMedia error: ', error); } navigator.mediaDevices.getUserMedia(constraints).then(handleSuccess).catch(handleError); return scene; }
讀到這里,這篇“Babylon使用麥克風并處理常見問題的方法是什么”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。