我正在尝试使用flutter的audioplayers库播放简单的声音。尝试执行此操作时,出现无法加载资产错误。我已经在此站点上浏览了几个相关的问题,所有这些问题似乎都只建议检查 pubspec.yaml 文件上的缩进并运行 flutter clean 。我已经做了几次,但没有结果。 Flutter Doctor 没有发现问题。
当我单击应该引起声音播放的按钮时,会收到以下消息:
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)
以下是应播放音频文件的代码:
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;
}
在这里调用方法:
void initState() {
super.initState();
initPlayer();
}
void startNewRoute() async {
await playSound('start.mp3');
// more code here
}
这是 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
文件的结构如下:
root
lib
[dart files]
images
[some images]
sounds
start.mp3
我在这里所做的事情和应该使用库文档解释它的方式之间找不到任何差异。谢谢您的任何帮助。
答案 0 :(得分:1)
按照设计,您应该添加“ assets /”以设置 pubspec.yaml ,如下所示:
assets:
- images/
- assets/sounds/start.mp3
不要忘记将这些文件添加到您的pubspec.yaml文件中:
颤振:资产:
- assets / explosion.mp3
有人有类似的问题here