I am new to flutter and currently learning flutter basics from tutorials online. I am currently building a simple app in which we can play an audio from our assets when TextButton is pressed. I have used audioplayers library for the same.
I am getting this lovely error since last one day which states "E/flutter (14878): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(AndroidAudioError, setDataSource failed., java.io.IOException: setDataSource failed."
I have tried my best researching about his error but couldn't find anything. Here is my 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'),
)
),
),
);
}
}
Here is my log:
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):
I have properly include the package in pubspec.yaml
I tried using different libraries for playing the audio file but I get some sort of errors every time. I have tried using different libraries like for generating random text and it worked fine.
I am following an online tutorial for flutter. I am doing the same as the instructor but getting the error.
You must add the source as AssetSource()
not DeviceFileSource()
as you play the file from the asset folder.
see the below snippet and update your onPressed()
code.
onPressed: ()
{
final player = AudioPlayer();
player.play(
// DeviceFileSource('assets/note1.wav'), // remove this line
AssetSource('note1.wav'), // add this line
);
},