1

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.

1 Answers1

0

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
                );
           },

DevPankajPatel
  • 258
  • 1
  • 8
  • I am still getting the error but it's a different one. It says: E/MediaPlayer(19206): Should have subtitle controller already set E/MediaPlayer(19206): error (1, -19) E/MediaPlayer(19206): Error (1,-19) E/MediaPlayer(19206): stop called in state 0 E/MediaPlayer(19206): error (-38, 0) I/flutter (19206): AudioPlayers Exception: AudioPlayerException( I/flutter (19206): AssetSource(path: note1.wav), I/flutter (19206): PlatformException(MEDIA_ERROR_UNKNOWN {what:1}, MEDIA_ERROR_UNKNOWN {extra:-19}, null, null) – Parul Pandey Jun 05 '23 at 10:14
  • Make sure your project has an `assets` folder and under this folder the `note1.wav` file is available. Try to Uninstall the older version of the app and clean the project and run it again. Note: also add assets in your pubspec.yaml file like ` assets: - assets/ ` – DevPankajPatel Jun 05 '23 at 10:17
  • thank you @DevPankajPatel , Actually there is some issue with the package and the audio is not getting played on the android emulator, I was able to play audio on my physical device and it worked fine. Thank you for your time!!! – Parul Pandey Jun 05 '23 at 11:33