oboard

oboard

https://oboard.eu.org/
github
email
tg_channel
medium
twitter
bilibili
steam_profiles
follow

Flutter audioplayers path pit Unhandled Exception: Unable to load asset:

At first, I wrote it like this

// Emit a sound prompt
var audioPlayer = AudioPlayer();
audioPlayer.setPlayerMode(PlayerMode.lowLatency);
audioPlayer.setReleaseMode(ReleaseMode.loop);
audioPlayer.play(AssetSource('sounds/1.mp3'));

But it kept throwing errors:

Unhandled Exception: Unable to load asset:

This is my directory
Directory structure

Later, I studied the source code carefully

 // read local asset from rootBundle
    final byteData = await rootBundle.load('$prefix$fileName');
```dart
I found a prefix
Looking further up
```dart
 /// 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/'});

I see, the default root directory is assets/
You can either move the audio files to assets/ or directly modify this prefix

audioPlayer.audioCache.prefix = '';

Adding the line above ⬆️ will work

Final code ⬇️

// Emit a sound prompt
var audioPlayer = AudioPlayer();
audioPlayer.setPlayerMode(PlayerMode.lowLatency);
audioPlayer.setReleaseMode(ReleaseMode.loop);
audioPlayer.audioCache.prefix = '';
audioPlayer.play(AssetSource('sounds/1.mp3'));
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.