我将 audio_service 与 just_audio 结合使用,并使用我在存储库中找到的教程代码构建了一个最小示例存储库。
如果我用于初始化 AudioPlayerHandler 的 URL 出于任何原因不正确(为了说明目的在示例代码中硬编码,请参见下面的链接)我无法找到检测错误的方法。有一个未处理的异常扰乱了状态,我不知道如何捕获它并在应用程序中采取适当的步骤让用户知道出现了错误。是否有回调或我可以听的东西?
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: (-1100) The requested URL was not found on this server.
#0 AudioPlayer._load (package:just_audio/just_audio.dart:785)
<asynchronous suspension>
#1 AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1351)
<asynchronous suspension>
最佳答案
这是 pub.dev/packages/just_audio 上“捕获播放器错误”部分的片段
try {
await player.setUrl("https://s3.amazonaws.com/404-file.mp3");
} on PlayerException catch (e) {
// iOS/macOS: maps to NSError.code
// Android: maps to ExoPlayerException.type
// Web: maps to MediaError.code
// Linux/Windows: maps to PlayerErrorCode.index
print("Error code: ${e.code}");
// iOS/macOS: maps to NSError.localizedDescription
// Android: maps to ExoPlaybackException.getMessage()
// Web/Linux: a generic message
// Windows: MediaPlayerError.message
print("Error message: ${e.message}");
} on PlayerInterruptedException catch (e) {
// This call was interrupted since another audio source was loaded or the
// player was stopped or disposed before this audio source could complete
// loading.
print("Connection aborted: ${e.message}");
} catch (e) {
// Fallback for all errors
print(e);
}
关于flutter - 如何捕捉音频文件 url 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69533575/