6

I'm adding sounds to my app using the audioplayers package but at first build no sound is played. After a Hot-restart dough when loading the file I get this exception:

E/flutter (32175): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Unable to load asset: assets/assets/click.mp3
E/flutter (32175): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
E/flutter (32175): <asynchronous suspension>
E/flutter (32175): #1      AudioCache._fetchAsset (package:audioplayers/audio_cache.dart:60:29)
E/flutter (32175): #2      AudioCache.fetchToMemory (package:audioplayers/audio_cache.dart:67:30)
E/flutter (32175): <asynchronous suspension>
E/flutter (32175): #3      AudioCache.load (package:audioplayers/audio_cache.dart:82:37)
E/flutter (32175): #4      AudioCache.getAbsoluteUrl (package:audioplayers/audio_cache.dart:140:23)
E/flutter (32175): #5      AudioCache.play (package:audioplayers/audio_cache.dart:103:24)
E/flutter (32175): #6      _MapScreenState.build.<anonymous closure> (package:fixit_cloud_biking/Screens/map_screen.dart:1239:46)
E/flutter (32175): #7      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (32175): #8      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (32175): #9      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (32175): #10     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (32175): #11     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (32175): #12     BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
E/flutter (32175): #13     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
E/flutter (32175): #14     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (32175): #15     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9)
E/flutter (32175): #16     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (32175): #17     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)
E/flutter (32175): #18     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7)
E/flutter (32175): #19     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (32175): #20     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (32175): #21     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (32175): #22     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (32175): #23     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (32175): #24     _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (32175): #25     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (32175): #26     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (32175): #27     _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (32175): #28     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (32175): 

I did put the files in the same assets folder that contains all the images I use for the icons so indentation in pubspec.yaml file is correct. The problem shouldn't be the file naming either as I use camel cased or single word naming(lesson learned from images naming with empty spaces..). One thing though worth to mention is that if I drag and drop files onto the assetsfolder I get an error. I tried deleting the folder, recreating it and then drag and drop works, but only once.. files added in a second moment throw the error. This is the button that should play the sound:

IconButton(
                        icon: Image.asset('assets/centerMapButton.png'),
                        iconSize: 60,
                        onPressed: () async {
                          await widget.cache.play('assets/click.mp3',
                              volume: 50, mode: PlayerMode.MEDIA_PLAYER);
                          print('Center map button pressed');
                          _mapController.move(userLocation, 16);
                        }),

and the AudioCache()is declared as AudioCache cache = new AudioCache();.

6
  • 1
    After adding files for the second time have you restarted (by stopping and relaunching) your app Apr 7, 2020 at 16:39
  • I tried both the app was running and when it wasn't. I did solve the file loading problem dough as it turns out that the path doesn't have to be specified as the plugin looks straight into the assets folder. play('assets/click.mp3') is wrong, play('click.mp3') works as expected.. I did noticed from the error Unable to load asset: assets/assets/click.mp3` that 'assets/' appeared twice, so I tried omitting it and it worked. Still I have to solve the drag and drop files adding problem, as copying them with finder works, but something is obviously wrong..
    – Vincenzo
    Apr 7, 2020 at 17:13
  • This is really weird Apr 7, 2020 at 17:28
  • yeap.. I'm gonna try moving the the project folder onto a different drive and see if makes any difference..
    – Vincenzo
    Apr 8, 2020 at 5:58
  • @Vincenzo That's actually by design. You can read the documentation here: github.com/luanpotter/audioplayers/blob/master/doc/…
    – Midhun MP
    Apr 9, 2020 at 8:51

4 Answers 4

5

This can happen in just_audio if you don't set the assets location in pubspec.yaml.

flutter:
  assets:
    - audio/

This tells Flutter that you have an audio folder in the root of your project where you keep your audio assets.

After doing that, you should be able to set the asset and play it like so:

await player.setAsset('audio/cow.mp3');
player.play();

The error can also happen if you spelled something in the path wrong, but I assume you already checked for that.

3

TRY CHANGING THIS assets/click.mp3 TO click.mp3

I had the same error neglecting the "assets/" in the path makes it play sounds from local seamlessly. Audioplayers assumes that its default path is assets/

1
  • Hi. Welcome and thanks for your answer. Fortunately I did discover that, and thanks to MidhumMP comment I saw why. Again thanks and welcome to the community.
    – Vincenzo
    Jun 1, 2020 at 22:05
1

Clearing the assets folder worked for me :

await AudioPlayer.clearAssetCache();

Clears the plugin's internal asset cache directory. Call this when the app's assets have changed to force assets to be re-fetched from the asset bundle.

0

The error is in assets U don't need to write it .

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 19, 2022 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.