2

I'm using the package: audioplayers: ^1.0.1

I'm trying to follow the migration guide of audioplayers found in this link: https://github.com/bluefireteam/audioplayers/blob/main/migration_guide.md

But I can't make it to work.

Here's my pubspec

flutter:
  assets:
    - assets/sounds/

If I do it like this, it can't load the assets.

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

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

But if I remove the assets, it is trying to find in the cache. I don't get why it is working. Please help to clarify. Thanks!

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)

This is the assets folder with the Pop (1).wav on it

Mr. Tacio
  • 442
  • 1
  • 7
  • 18

2 Answers2

3

Ok so what i see from the package structure.

If you Go into the AssetSource class which looks as follows :

source.dart file

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

Here if you go inside the setSourceAsset method :

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

if you see the line

 final url = await audioCache.load(path);

so this in this AudioCache class if you see the constructor :


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

So the prefix is assets added as a default in the constructor. So you don't have to call it every time only the file name followed by the folder if you are adding in any case.

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34
1

After removing the spaces in the name. It is now working. Thanks to @Olek L. and @farouk osama

Mr. Tacio
  • 442
  • 1
  • 7
  • 18