Flutter3 × Audioplayers で音源が再生できません
解決したいこと
Flutter3で外部パッケージのaudioplayersを使って音源を再生する機能を実装中です。
なぜか音源が再生されないので、解決策が欲しいです。
例)
Flutter3を用いて実装しています。エディターはAndroidStudioです。
該当するソースコード
lib/main.dart
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final player = AudioPlayer();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
await player.play(
DeviceFileSource('assets/audios/note2.wav'),
); // will immediately start playing
},
child: Text('Click Me!'),
),
),
);
}
}
pubspec.yaml
name: smart_xylophone
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.17.1 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
audioplayers: ^1.0.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
assets:
- assets/audios/
Performing hot restart...
Syncing files to device Android SDK built for arm64...
Restarted application in 935ms.
V/MediaPlayer( 7675): resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer( 7675): cleanDrmObj: mDrmObj=null mDrmSessionId=null
D/AudioPlayers( 7675): Unexpected error!
D/AudioPlayers( 7675): java.io.IOException: setDataSource failed.
D/AudioPlayers( 7675): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1191)
D/AudioPlayers( 7675): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1164)
D/AudioPlayers( 7675): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1131)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.player.MediaPlayerPlayer.setSource(MediaPlayerPlayer.kt:52)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:29)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:87)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:23)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
D/AudioPlayers( 7675): at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:58)
D/AudioPlayers( 7675): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
D/AudioPlayers( 7675): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
D/AudioPlayers( 7675): at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
D/AudioPlayers( 7675): at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
D/AudioPlayers( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
D/AudioPlayers( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
D/AudioPlayers( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
D/AudioPlayers( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
E/flutter ( 7675): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(Unexpected error!, setDataSource failed., java.io.IOException: setDataSource failed.
E/flutter ( 7675): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1191)
E/flutter ( 7675): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1164)
E/flutter ( 7675): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1131)
E/flutter ( 7675): at xyz.luan.audioplayers.source.UrlSource.setForMediaPlayer(UrlSource.kt:16)
E/flutter ( 7675): at xyz.luan.audioplayers.player.MediaPlayerPlayer.setSource(MediaPlayerPlayer.kt:52)
E/flutter ( 7675): at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:29)
E/flutter ( 7675): at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:87)
E/flutter ( 7675): at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:23)
E/flutter ( 7675): at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
E/flutter ( 7675): at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:39)
E/flutter ( 7675): at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:58)
E/flutter ( 7675): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/flutter ( 7675): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
E/flutter ( 7675): at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
E/flutter ( 7675): at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
E/flutter ( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
E/flutter ( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
E/flutter ( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
E/flutter ( 7675): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
E/flutter ( 7675): , null)
E/flutter ( 7675): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 7675): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter ( 7675): <asynchronous suspension>
E/flutter ( 7675): #2 AudioPlayer.play (package:audioplayers/src/audioplayer.dart:111:5)
E/flutter ( 7675): <asynchronous suspension>
E/flutter ( 7675): #3 _MyHomePageState.build.<anonymous closure> (package:smart_xylophone/main.dart:45:13)
E/flutter ( 7675): <asynchronous suspension>
E/flutter ( 7675):
buildして「Click Me!」というボタンをタップすると、コンソールには上のようなメッセージが出てきます。
自分で試したこと
- pubspec.yamlに音源があるディレクトリを正しく記載していないのではないかと思いましたが正しい記述でした。
- Flutterやaudioplayersのバージョンに関係あるかと思いましたが、調べた限り異常は見つかりませんでした。
- 単純な記述ミスも探しましたが、見つかりませんでした。
打つ手がなく困っています。よろしくお願いします。
0