我是 Flutter 新手,目前正在通过在线教程学习 Flutter 基础知识。我目前正在构建一个简单的应用程序,当按下 TextButton 时,我们可以从我们的资源中播放音频。我已经使用了 audioplayers 库来实现同样的目的。
自上一天以来,我收到了这个可爱的错误,其中指出“E/flutter (14878): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(AndroidAudioError, setDataSource failed., java.io .IOException:setDataSource失败。”
我已尽力研究他的错误,但找不到任何东西。 这是我的 main.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 的在线教程。 我和教练做的一样,但出现错误。
在播放资源文件夹中的文件时,您必须将源添加为
AssetSource()
而不是 DeviceFileSource()
。
请参阅下面的代码片段并更新您的
onPressed()
代码。
onPressed: ()
{
final player = AudioPlayer();
player.play(
// DeviceFileSource('assets/note1.wav'), // remove this line
AssetSource('note1.wav'), // add this line
);
},
在处理 udemy 项目时遇到这个问题xylophone_flutter
我的flutter版本是3.16.9
flutter --version
Flutter 3.16.9 • channel stable • https://github.com/flutter/flutter.git
我的dart版本是3.2.6
dart --version
Dart SDK version: 3.2.6 (stable) (Wed Jan 24 13:41:58 2024 +0000) on "macos_x64"
音频播放器依赖版本
audioplayers: ^5.2.1
下面是完整的工作代码。
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(XylophoneApp());
}
class XylophoneApp extends StatelessWidget {
final player = AudioPlayer();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Xylophone')),
body: SafeArea(
child: Center(
child: TextButton(
onPressed: () {
player.play(AssetSource('note2.wav'));
},
child: Text("Play"),
),
)),
),
);
}
}
正如我们所见,
AssetSource
用于加载与应用程序捆绑在一起的音频。当您将音频存储在共享磁盘位置时,使用 DeviceSource
。
默认情况下,库只会查看 assets 文件夹。
因此请确保您的语音笔记仅位于资产文件夹中。并且它也注册在 pubspec.yaml 中
flutter:
uses-material-design: true
assets:
- assets/