Babylon使用麦克风并处理常见问题解决
作者:方寸匠心 发布时间:2024-04-29 13:25:53
标签:Babylon,麦克风,问题处理
引言
在Babylon.js中媒体设备流使用起来有很多坑,我们将在本文中逐一说明这些坑并提供相应的解决方案。
问题1:Property 'constraints' does not exist on type 'Window & typeof globalThis'.
在比较新的浏览器中,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 常量。
问题2: Property 'stream' does not exist on type 'Window & typeof globalThis'.ts(2339)
如果在使用媒体设备流时,你遇到了“Property 'stream' does not exist on type 'Window & typeof globalThis'.ts(2339)” 这个问题出现在TypeScript项目中。因为该类型Script不认识window.stream, 所以我们需要将它声明为any类型。解决方法如下:
(window as any).stream = stream;
这让编译器知道 window 对象现在具有一个名为stream的属性。
问题3: Property 'oninactive' does not exist on type 'MediaStream'.ts(2339)
在较新版本的Web API中,MediaStream
对象不再包含oninactive
属性,需要更改事件监听方式。 解决方法如下:
stream.addEventListener('inactive', () => {
console.log('Stream ended ... ...');
});
而不是
stream.oninactive = () => {
console.log('Stream ended ... ...');
};
此更改暴露了更多事件,同时也需要更改您的代码来处理它们。
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;
}
来源:https://juejin.cn/post/7216992973418774589


猜你喜欢
- Pytest-sugar是一款用来改善控制台显示的插件,增加了进度条显示,使得在用例执行过程中可以看到进度条,而且进度条是根据用例是否通过标
- 前言大家都知道其实学习Django非常简单,几乎不用花什么精力就可以入门了。配置一个url,分给一个函数处理它,返回response,几乎都
- $array=explode(separator,$string); $string=implode(glue,$array);使用和理解这
- WeUI 是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信内网页和微信小程序量身设计,令用户的使用感知更加统一。
- 本篇文章是作者关于在学习了《Python学习手册》以后,分享的学习心得,在此之前,我们先给大家分享一下这本书:下载地址:Python学习手册
- 在我上一篇文章,我搭了一个框架,模拟了Flask网站上“@app.route(‘/')”第一条例子的行为。如果你错过了那篇“这不是魔
- 如下所示:# coding:utf-8'''求两个字符串的最长公共子串思想:建立一个二维数组,保存连续位相同与否的状
- python3 cmp实现python3移除了cmp()函数,但提供了六个丰富的比较运算符,详见此处import operator &nbs
- 产品是一款服务于人力资源的SaaS在线服务,面向HR有Web Android/iOS 小程序多个客户端后端采用RESTful风格API来提供
- 本文实例为大家分享了python实现桌面壁纸切换功能的具体实现方法,供大家参考,具体内容如下大体分为两个部分一、利用爬虫爬取壁纸第一部分爬取
- 总说由于pytorch 0.4版本更新实在太大了, 以前版本的代码必须有一定程度的更新. 主要的更新在于 Variable和Tensor的合
- 其实发这篇博感觉并没有什么用,太简单了,会的人不屑看,不会的人自已动动脑子也想到了。但是看着自已的博客已经这么久没更,真心疼~。粗略算下一篇
- 表结构如下面代码创建 CREATE TABLE test_tb ( TestId int not null identity(1,1) pr
- 环境与开发工具在抓包的时候,开始使用的是Chrome开发工具中的Network,结果没有抓到,后来使用Fiddler成功抓取数据。下面逐步来
- HTML:<%-- 右键菜单 --%><div id="zTreeRightMenuContainer"
- datetime64与unix时间戳互转在用pandas处理数据时,经常要处理一些时间类型数据,经常把pandas时间类型与datetime
- 1. set 的基本内容1.基本特点(1) 无序性(2) 确定性(3) 不重复性2.set() 实质内部进行 可迭代性的 for 循环例子:
- 我就废话不多说了,大家还是直接看代码吧~import pandas as pdimport numpy as npcolumns = [[&
- 一、概述变量的功能是存储用户的数据二、声明变量Go语言的每一个变量都拥有自己的类型,必须经过声明才能开始用变量的声明格式:var <变
- 当在一个分支的开发工作未完成,却又要切换到另外一个分支进行开发的时候,除了commit原分支的代码改动的方法外,我觉得git st