Unhandled Exception: PlatformException(AndroidAudioError, setDataSource failed., java.io.IOException: setDataSource failed

huangapple go评论76阅读模式
英文:

Unhandled Exception: PlatformException(AndroidAudioError, setDataSource failed., java.io.IOException: setDataSource failed

问题

我是新手学习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 failed.

我已经尽力研究这个错误,但找不到任何解决方法。

以下是我的main.dart代码:

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';

void main() {
  runApp(Xylophone());
}

class Xylophone extends StatefulWidget {
  const Xylophone({Key? 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(
                '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。
我和教程中的教练做的一样,但是出现了错误。

英文:

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 &#39;package:flutter/material.dart&#39;;
import &#39;package:audioplayers/audioplayers.dart&#39;;
void main() {
runApp(xylophone());
}
class xylophone extends StatefulWidget {
const xylophone({super.key});
@override
State&lt;xylophone&gt; createState() =&gt; _xylophoneState();
}
class _xylophoneState extends State&lt;xylophone&gt; {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: TextButton(
onPressed: ()  {
final player = AudioPlayer();
player.play(
DeviceFileSource(&#39;assets/note1.wav&#39;),
);
},
child: Text(&#39;Click Me&#39;),
)
),
),
);
}
}

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): &lt;asynchronous suspension&gt;
E/flutter (14878): #2      AudioPlayer._completePrepared (package:audioplayers/src/audioplayer.dart:301:5)
E/flutter (14878): &lt;asynchronous suspension&gt;
E/flutter (14878): #3      AudioPlayer.setSourceDeviceFile (package:audioplayers/src/audioplayer.dart:325:5)
E/flutter (14878): &lt;asynchronous suspension&gt;
E/flutter (14878): #4      AudioPlayer.setSource (package:audioplayers/src/audioplayer.dart:284:5)
E/flutter (14878): &lt;asynchronous suspension&gt;
E/flutter (14878): #5      AudioPlayer.play (package:audioplayers/src/audioplayer.dart:182:5)
E/flutter (14878): &lt;asynchronous suspension&gt;
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

得分: 0

你必须将源更改为AssetSource(),而不是DeviceFileSource(),因为你要播放来自资产文件夹的文件。

请查看下面的代码片段并更新你的onPressed()代码。

onPressed: ()  
{
    final player = AudioPlayer();
    player.play(
        // DeviceFileSource('assets/note1.wav'), // 移除此行
        AssetSource('note1.wav'), // 添加此行
    );
},
英文:

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(&#39;assets/note1.wav&#39;), // remove this line
AssetSource(&#39;note1.wav&#39;), // add this line
);
},

huangapple
  • 本文由 发表于 2023年6月5日 17:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定