I am attempting to play a simple sound using flutter's audioplayers library. When I attempt to, I get an Unable to load asset error. I've gone through several related questions on this site, and all of them seem to only suggest checking the indentation on the pubspec.yaml file and running flutter clean. I have done this several times and have had no results. Flutter Doctor finds no issues.
When I click the button that should cause the sound to play, I receive this:
E/flutter (22727): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Unable to load asset: assets/sounds/start.mp3
E/flutter (22727): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
E/flutter (22727): <asynchronous suspension>
E/flutter (22727): #1 AudioCache._fetchAsset (package:audioplayers/audio_cache.dart:60:29)
E/flutter (22727): #2 AudioCache.fetchToMemory (package:audioplayers/audio_cache.dart:67:30)
E/flutter (22727): <asynchronous suspension>
E/flutter (22727): #3 AudioCache.load (package:audioplayers/audio_cache.dart:82:37)
E/flutter (22727): #4 AudioCache.getAbsoluteUrl (package:audioplayers/audio_cache.dart:140:23)
E/flutter (22727): #5 AudioCache.play (package:audioplayers/audio_cache.dart:103:24)
E/flutter (22727): #6 playSound (package:tick_tok_bio/player.dart:14:20)
E/flutter (22727): #7 MapsState.startNewRoute (package:tick_tok_bio/gps_tracking.dart:122:13)
E/flutter (22727): #8 MapsState.startStop.<anonymous closure> (package:tick_tok_bio/gps_tracking.dart:240:13)
E/flutter (22727): #9 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:779:19)
E/flutter (22727): #10 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:862:36)
E/flutter (22727): #11 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (22727): #12 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:504:11)
E/flutter (22727): #13 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:282:5)
E/flutter (22727): #14 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:217:7)
E/flutter (22727): #15 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:475:9)
E/flutter (22727): #16 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (22727): #17 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:122:9)
E/flutter (22727): #18 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (22727): #19 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:120:18)
E/flutter (22727): #20 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:106:7)
E/flutter (22727): #21 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (22727): #22 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (22727): #23 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (22727): #24 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (22727): #25 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (22727): #26 _rootRunUnary (dart:async/zone.dart:1196:13)
E/flutter (22727): #27 _CustomZone.runUnary (dart:async/zone.dart:1085:19)
E/flutter (22727): #28 _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)
E/flutter (22727): #29 _invoke1 (dart:ui/hooks.dart:275:10)
E/flutter (22727): #30 _dispatchPointerDataPacket (dart:ui/hooks.dart:184:5)
Here is the code that should play the audio file:
import 'package:audioplayers/audio_cache.dart';
import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
AudioPlayer advancedPlayer;
AudioCache audioCache;
void initPlayer() {
advancedPlayer = AudioPlayer();
audioCache = AudioCache(fixedPlayer: advancedPlayer, prefix: 'sounds/');
}
Future<void> playSound(String path) async {
await audioCache.play(path);
return;
}
Here is where the methods are called:
void initState() {
super.initState();
initPlayer();
}
void startNewRoute() async {
await playSound('start.mp3');
// more code here
}
Here is the assets section of pubspec.yaml
flutter:
uses-material-design: true
assets:
- images/
- sounds/start.mp3
fonts:
- family: RobotoMono
fonts:
- asset: fonts/RobotoMono-Regular.ttf
- asset: fonts/RobotoMono-Bold.ttf
And the files are structured like this:
root
lib
[dart files]
images
[some images]
sounds
start.mp3
I can't find any discrepancies between what I've done here and the way the documentation of the library explains it should be used. Any help is appreciated, thanks.
As per by design, you should be adding "assets/" setting your pubspec.yaml like this:
assets:
- images/
- assets/sounds/start.mp3
Don't forget to add these files to your pubspec.yaml file:
flutter: assets:
- assets/explosion.mp3
someone had similar issue here