A new Flutter project.
In the project we have used just_audio and audio_service packages
audio_service package works to handle audio in the background and show the media notification, it offer a framework by implement the [BaseAudioHandler] class using any audio package, here we used just_audio package
class MyAudioHandler extends BaseAudioHandler {
final _player = AudioPlayer();
@override
Future<void> play() => _player.play();
@override
Future<void> pause() => _player.pause();
@override
Future<void> seek(Duration position) => _player.seek(position);
@override
Future<void> setRepeatMode(AudioServiceRepeatMode repeatMode) async {
return _player.setLoopMode(
AudioPackagesHelper.serviceToJustAudioLoopMode(repeatMode),
);
}
@override
Future<void> skipToNext() async {
return _player.seekToNext();
}
@override
Future<void> skipToPrevious() async {
return _player.seekToPrevious();
}
//...
}
we add required changes for android and ios after in the main methode we initialize the audio service
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await setupInjection();
runApp(const SpotifyApp());
}
final sl = GetIt.instance;
Future<void> setupInjection() async {
final audioService = await initAudioService();
sl.registerLazySingleton<AudioHandler>(() => audioService);
sl.registerLazySingleton(() => SongRepository(audioHandler: sl()));
sl.registerFactory(() => MusicPlayerCubit(songRepository: sl()));
}
- packages on pub.dev audio_service just_audio
- demo project of the package owner demo_project
- music app be maxonflutter music_app
- medium articles one two