我是新的Flutter和目前学习Flutter基础教程在线。我目前正在构建一个简单的应用程序,当按下TextButton时,我们可以从我们的资产中播放音频。我用audioplayers库做了同样的事情。
我得到这个可爱的错误,因为最后一天,其中指出“E/Flutter(14878):[错误:flutter/runtime/dart_vm_initializer.cc(41)]无法处理的异常:PlatformException(AndroidAudioError,setDataSource failed.,java.io.IOException:setDataSource失败。”
我已经尽了最大的努力去研究他的错误,但什么也找不到。这里是我的主要.dart:
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
void main() {
runApp(xylophone());
}
class xylophone extends StatefulWidget {
const xylophone({super.key});
@override
State<xylophone> createState() => _xylophoneState();
}
class _xylophoneState extends State<xylophone> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: TextButton(
onPressed: () {
final player = AudioPlayer();
player.play(
DeviceFileSource('assets/note1.wav'),
);
},
child: Text('Click Me'),
)
),
),
);
}
}
以下是我的日志:
E/flutter (14878): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(AndroidAudioError, setDataSource failed., java.io.IOException: setDataSource failed.
E/flutter (14878): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1086)
E/flutter (14878): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
E/flutter (14878): at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16)
E/flutter (14878): at xyz.luan.audioplayers.player.MediaPlayerPlayer.setSource(MediaPlayerPlayer.kt:53)
E/flutter (14878): at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:29)
E/flutter (14878): at xyz.luan.audioplayers.AudioplayersPlugin.methodHandler(AudioplayersPlugin.kt:126)
E/flutter (14878): at xyz.luan.audioplayers.AudioplayersPlugin.access$methodHandler(AudioplayersPlugin.kt:29)
E/flutter (14878): at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:50)
E/flutter (14878): at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:50)
E/flutter (14878): at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:75)
E/flutter (14878): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/flutter (14878): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
E/flutter (14878): at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
E/flutter (14878): at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
E/flutter (14878): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
E/flutter (14878): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
E/flutter (14878): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
E/flutter (14878): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
E/flutter (14878): , null)
E/flutter (14878): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
E/flutter (14878): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)
E/flutter (14878): <asynchronous suspension>
E/flutter (14878): #2 AudioPlayer._completePrepared (package:audioplayers/src/audioplayer.dart:301:5)
E/flutter (14878): <asynchronous suspension>
E/flutter (14878): #3 AudioPlayer.setSourceDeviceFile (package:audioplayers/src/audioplayer.dart:325:5)
E/flutter (14878): <asynchronous suspension>
E/flutter (14878): #4 AudioPlayer.setSource (package:audioplayers/src/audioplayer.dart:284:5)
E/flutter (14878): <asynchronous suspension>
E/flutter (14878): #5 AudioPlayer.play (package:audioplayers/src/audioplayer.dart:182:5)
E/flutter (14878): <asynchronous suspension>
E/flutter (14878):
我已经正确地将包包含在pubspec.yaml中
我尝试使用不同的库来播放音频文件,但每次都会出现某种错误。我试过使用不同的库,如生成随机文本,它工作得很好。
我正在学习Flutter的在线教程。我做了和老师一样的事情,但是出错了。
1条答案
按热度按时间js81xvg61#
在播放资源文件夹中的文件时,必须将源添加为
AssetSource()
,而不是DeviceFileSource()
。请参阅下面的代码片段并更新您的
onPressed()
代码。