【问题标题】:Assets is being loaded in cache but not found for AudioPlayer资产正在缓存中加载,但未找到 AudioPlayer
【发布时间】:2022-09-24 21:25:21
【问题描述】:

我正在使用包:音频播放器:^1.0.1

我正在尝试遵循此链接中找到的音频播放器迁移指南:https://github.com/bluefireteam/audioplayers/blob/main/migration_guide.md

但我无法让它工作。

这是我的 pubspec

flutter:
  assets:
    - assets/sounds/

如果我这样做,它无法加载资产.

  final playerSound = AudioPlayer();
  await playerSound.setSource(AssetSource(\'assets/sounds/Pop (1).wav\'));

Unhandled Exception: Unable to load asset: assets/assets/sounds/Pop (1).wav

但是如果我删除资产,它试图在缓存中查找。我不明白它为什么起作用。请帮忙澄清。谢谢!

final playerSound = AudioPlayer();
  await playerSound.setSource(AssetSource(\'sounds/Pop (1).wav\'));

java.io.FileNotFoundException: /data/user/0/com.MyName.MyApp/cache/sounds/Pop%20(1).wav: open failed: ENOENT (No such file or directory)

  • 你能以图片格式显示资产文件夹吗?
  • 我添加了图片

标签: flutter


【解决方案1】:

好的,所以我从包结构中看到了。

如果您进入如下所示的 AssetSource 类:

source.dart 文件

/// Source representing the path of an application asset in your Flutter
/// "assets" folder.
/// Note that a prefix might be applied by your [AudioPlayer]'s audio cache
/// instance.
class AssetSource extends Source {
  final String path;
  AssetSource(this.path);

  @override
  Future<void> setOnPlayer(AudioPlayer player) {
    return player.setSourceAsset(path);
  }
}

在这里,如果您进入 setSourceAsset 方法:

 /// Sets the URL to an asset in your Flutter application.
  /// The global instance of AudioCache will be used by default.
  ///
  /// The resources will start being fetched or buffered as soon as you call
  /// this method.
  Future<void> setSourceAsset(String path) async {
    final url = await audioCache.load(path);
    return _platform.setSourceUrl(playerId, url.path, isLocal: true);
  }

如果你看到这条线

 final url = await audioCache.load(path);

因此,如果您看到构造函数,则在此 AudioCache 类中:


  /// This is the path inside your assets folder where your files lie.
  ///
  /// For example, Flame uses the prefix 'assets/audio/'
  /// (you must include the final slash!).
  /// The default prefix (if not provided) is 'assets/'
  /// Your files will be found at <prefix><fileName> (so the trailing slash is
  /// crucial).
  String prefix;

  AudioCache({this.prefix = 'assets/'});

所以前缀是在构造函数中作为默认添加的资产。因此,如果您在任何情况下添加,您不必每次只调用文件名后跟文件夹。

【讨论】:

  • 但是当我这样做时, await playerSound.setSource(AssetSource('sounds/Pop (1).wav'));它有一个未找到的错误。它正在缓存中查找。我还是不明白。
猜你喜欢
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 2020-07-06
  • 1970-01-01
相关资源
最近更新 更多