Flutter Audioplayers 无法加载资产
Flutter Audioplayers Unable to load asset
我正在尝试使用 flutter 的音频播放器库播放简单的声音。当我尝试这样做时,出现 Unable to load asset 错误。我在这个网站上浏览了几个相关问题,所有这些问题似乎只建议检查 pubspec.yaml 文件和 运行 扑干净。我已经这样做了好几次,但没有结果。 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
我找不到我在这里所做的与库的文档解释它应该使用的方式之间的任何差异。感谢任何帮助,谢谢。
根据设计,您应该添加“资产/”设置您的 pubspec.yaml,如下所示:
assets:
- images/
- assets/sounds/start.mp3
Don't forget to add these files to your pubspec.yaml file:
flutter: assets:
- assets/explosion.mp3
有人有类似的问题here
我正在尝试使用 flutter 的音频播放器库播放简单的声音。当我尝试这样做时,出现 Unable to load asset 错误。我在这个网站上浏览了几个相关问题,所有这些问题似乎只建议检查 pubspec.yaml 文件和 运行 扑干净。我已经这样做了好几次,但没有结果。 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
我找不到我在这里所做的与库的文档解释它应该使用的方式之间的任何差异。感谢任何帮助,谢谢。
根据设计,您应该添加“资产/”设置您的 pubspec.yaml,如下所示:
assets:
- images/
- assets/sounds/start.mp3
Don't forget to add these files to your pubspec.yaml file:
flutter: assets:
- assets/explosion.mp3
有人有类似的问题here