【问题标题】:How to catch an audio file url issue?如何捕捉音频文件 url 问题?
【发布时间】:2021-11-30 15:20:40
【问题描述】:

我将 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>

【问题讨论】:

  • 官方的 just_audio 示例不是演示了如何做到这一点吗?另外,README 页面没有给出一个例子吗?
  • 哦,我明白了。他们将其转换为 await 并将其包装在 try-catch 中。对于未来的读者:请参阅pub.dev/packages/just_audio上的“捕捉播放器错误”部分

标签: flutter just-audio audio-service


【解决方案1】:

这是来自 pub.dev/packages/just_audio 上“捕捉播放器错误”部分的 sn-p

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);
}

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2012-07-13
    相关资源
    最近更新 更多