Flutter-AssetsAudioPlayer
Flutter-AssetsAudioPlayer copied to clipboard
Failed to open a playlist
Version
Flutter: 3.0.5
file_picker: 5.0.1
assets_audio_player: 3.0.5
Platform
Android 12 64-bit
ZenFone 8 Flip (ASUS I004D
)
Description
The player can open a audio file and play it normally. However, an error occurs when the player opens a playlist.
Error
W/System.err(13498): com.github.florent37.assets_audio_player.playerimplem.PlayerFinder$NoPlayerFoundException
W/System.err(13498): at com.github.florent37.assets_audio_player.playerimplem.PlayerFinder._findWorkingPlayer(PlayerFinder.kt:70)
W/System.err(13498): at com.github.florent37.assets_audio_player.playerimplem.PlayerFinder._findWorkingPlayer(PlayerFinder.kt:88)
W/System.err(13498): at com.github.florent37.assets_audio_player.playerimplem.PlayerFinder.access$_findWorkingPlayer(PlayerFinder.kt:26)
W/System.err(13498): at com.github.florent37.assets_audio_player.playerimplem.PlayerFinder$_findWorkingPlayer$1.invokeSuspend(Unknown Source:15)
W/System.err(13498): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
W/System.err(13498): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
W/System.err(13498): at android.os.Handler.handleCallback(Handler.java:938)
W/System.err(13498): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(13498): at android.os.Looper.loopOnce(Looper.java:241)
W/System.err(13498): at android.os.Looper.loop(Looper.java:358)
W/System.err(13498): at android.app.ActivityThread.main(ActivityThread.java:8006)
W/System.err(13498): at java.lang.reflect.Method.invoke(Native Method)
W/System.err(13498): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
W/System.err(13498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1009)
I/flutter (13498): PlatformException(OPEN, null, null, null)
E/flutter (13498): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(OPEN, null, null, null)
E/flutter (13498):
Code
Here's a simple code to reproduct the error.
Run the app and press the button at bottom right corner, then pick a directory with audio files in it.
If I remove the code await _audioPlayer.open(Playlist(...));
, the error will not appear.
import 'dart:io';
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@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 _audioPlayer = AssetsAudioPlayer();
String _text = '';
void _onButtonPressed() async {
// Pick a directory
String? direPath = await FilePicker.platform.getDirectoryPath();
if (direPath == null) return;
// Get all audio files from the directory
final directory = await Directory(direPath)
.list(recursive: false, followLinks: false)
.toList();
final audios = <Audio>[]; // playlist
for (var element in directory) {
final path = element.uri.toString();
if (path.endsWith('.mp3') && element is File) audios.add(Audio(path));
}
// Open the playlist
await _audioPlayer.open(Playlist(audios: audios), autoStart: true, loopMode: LoopMode.playlist);
debugPrintThrottled('Completed');
setState(() {
_text = direPath;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Directory:',
),
Text(
'$_text',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _onButtonPressed,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}