1

我正在将 audio_service 与 just_audio 一起使用,并使用我在 repo 中找到的教程代码构建了一个最小的示例 repo。

如果我用于初始化 AudioPlayerHandler 的 URL 出于任何原因不正确(示例代码中的硬编码仅用于说明目的,请参见下面的链接)我无法找到检测错误的方法。有一个未处理的异常会打乱状态,我不知道如何捕捉它并在应用程序中采取适当的步骤让用户知道出现了错误。有回调或我可以听的东西吗?

https://github.com/SarvagyaVaish/audio_service_bad_url_example/blob/master/lib/audio_player_handler.dart#L13

[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>

4

1 回答 1

0

这是 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);
}
于 2021-10-13T17:56:43.713 回答