【问题标题】:mediaDevices.enumerateDevices() Generates Runtime Error in BrowsermediaDevices.enumerateDevices() 在浏览器中生成运行时错误
【发布时间】:2020-01-07 15:46:25
【问题描述】:

我正在尝试使用 Dart 2.7.0 SDK 在浏览器中通过 Web 应用程序从输入源捕获音频。我希望能够识别系统上可用的音频源,以便用户可以从中进行选择(而不是完全依赖默认源):

captureAudio() async{
   List<MediaDeviceInfo> sources = await window.navigator.mediaDevices.enumerateDevices();

    // Do more stuff here

    // Note that there are numerous differences in media APIs between those listed
    // for Dart and the JavaScript APIs shown on MDN. Those differences may or may not
    // have any effect on this error
}

上面的代码行编译成功,但无论是否处于调试模式,都会在 Webstorm 2019.3.1 中的 Webdev 提供的 Chrome 79.0.3945.88 中生成错误(注意:MDN 显示相应的 JS 方法调用应该在 Chrome 中工作) :

Uncaught (in promise) Error: Type 'List<dynamic>' should be 'List<MediaDeviceInfo>' to implement expected type 'FutureOr<List<MediaDeviceInfo>>'.
DartError                   dart_sdk.js:5172
throw_                      dart_sdk.js:3880
castError                   dart_sdk.js:3840
cast                        dart_sdk.js:4159
check_FutureOr              dart_sdk.js:27380
(anonymous function)        dart_sdk.js:52085
Async call from Promise.then
promiseToFuture             dart_sdk.js:52087
[dartx.enumerateDevices]    dart_sdk.js:79203
captureAudio                capturewebaudio.dart:51
runBody                     dart_sdk.js:32202
_async                      dart_sdk.js:32230
captureAudio                capturewebaudio.dart:50
(anonymous function)        capturewebaudio.dart:40
_checkAndCall               dart_sdk.js:4084
dcall                       dart_sdk.js:4089
(anonymous function)        dart_sdk.js:97072

关于这个主题的 Dart 文档基本上不存在,并且考虑到 Dart 和 JS API 的变化,我不确定这个问题的解决方案是什么。

【问题讨论】:

  • 这看起来可能是 API 中的错误。你应该在dart-lang/sdk Github 页面上的一个问题中发布这个。
  • 在 dartpad 中代码没有给出任何错误,但只返回 null
  • 谢谢你们俩。我在 dartpad 上试过这个(好主意,顺便说一句),据我所知, enumerateDevices() 调用甚至没有正确完成。如果源被初始化为 null,则它保持为 null。如果它被初始化为一个空的 List 对象,那么它将保持为一个空的 List 对象。我在 Chrome 和 Firefox 上都试过 dartpad。 Safari 不支持通过导航器获取 mediaDevices 对象(据我了解,这是一种已弃用的方法),因此我无法在 dartpad 上有效地尝试该浏览器。我想我会提交一份错误报告,看看会发生什么。谢谢!

标签: dart web-audio-api mediadevices


【解决方案1】:

这似乎是 Dart SDK 中的一个错误。请参阅 Dart SDK 问题列表中的错误 39627。修复程序正在开发过程中进行。

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2021-11-13
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多