无法加载资产:加载.mp3文件时出错Flutter

x8diyxa7  于 2023-10-22  发布在  Flutter
关注(0)|答案(5)|浏览(157)

我正在使用audioplayers包向应用程序添加声音,但在第一次构建时没有播放声音。在热重启面团加载文件时,我得到这个异常:

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):

我确实把文件放在同一个资产文件夹中,其中包含我用于图标的所有图像,因此pubspec.yaml文件中的缩进是正确的。问题不应该是文件命名,因为我使用 Camel 大小写或单个单词命名(从空格命名的图像中吸取的教训..)。值得一提的是,如果我将文件拖放到assets文件夹中,我会得到一个错误。我试着删除文件夹,重新创建它,然后拖放工作,但只有一次。在第二时刻添加的文件抛出错误。这是播放声音的按钮:

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

AudioCache()被声明为AudioCache cache = new AudioCache();

tvokkenx

tvokkenx1#

如果您没有在pubspec.yaml中设置资产位置,则在just_audio中可能会发生这种情况。

flutter:
  assets:
    - audio/

这告诉Flutter你在项目的根目录下有一个audio文件夹,你可以在那里保存音频资源。
在这样做之后,您应该能够设置资产并像这样播放它:

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

如果您在路径中拼写错误,也可能发生错误,但我假设您已经检查过了。

o2gm4chl

o2gm4chl2#

尝试将此资产/click.mp3更改为click.mp3
我也犯了同样的错误,忽略了路径中的“assets/”,这使得它可以无缝地播放本地的声音。audioplayer假定其默认路径为assets/

0vvn1miw

0vvn1miw3#

清除资产文件夹对我来说很有效:

await AudioPlayer.clearAssetCache();

清除插件的内部资源缓存目录。当应用程序的资产发生更改时调用此函数,以强制从资产包中重新获取资产。

fsi0uk1n

fsi0uk1n4#

错误在assets中,不需要写。

093gszye

093gszye5#

如果您的资产路径为:lib/assets/audio/
在您的文件pubspec.yaml中,
assets:- lib/assets/audio/

class Sound {
  static final AudioPlayer player = AudioPlayer();

  static void playSound(int option){
    player.audioCache.prefix = 'lib/assets/audio/';
    player.setPlayerMode(PlayerMode.mediaPlayer);
    player.play(AssetSource('note$option.wav'), mode: PlayerMode.lowLatency);
    player.audioCache.clearAll();
  }
}

相关问题