Reputation: 2256
Tagging this with the appropriate tag so that the repo author can see it.
When my app uses an audio file saved on the user's phone, it works just fine the first time. It shows the audio source as:
justAudioPlayer.audioSource: Instance of 'ProgressiveAudioSource'
playerState: playing=true,processingState=ProcessingState.ready
When I hit the back button (Navigator.pop()
) and then go back into the screen and try to play the audio source again, the audio source is now null:
justAudioPlayer.audioSource: null
playerState: playing=true,processingState=ProcessingState.idle
Upvotes: 0
Views: 1611
Reputation: 1
/* To ensure that the audio playback continues using the same instance of just_audio when you leave and return to the playback page, you can maintain a single instance of AudioPlayer in a global manager or service provider to make it globally accessible in your application. */
// Create a class to manage the AudioPlayer instance. For example, you can create a file called audio_player_service.dart:
import 'package:just_audio/just_audio.dart';
class AudioPlayerService {
static AudioPlayer? _audioPlayer;
static AudioPlayer get audioPlayer {
_audioPlayer ??= AudioPlayer();
return _audioPlayer!;
}
static void dispose() {
_audioPlayer?.dispose();
_audioPlayer = null;
}
}
/*
Use AudioPlayerService in your audio handler to access the unique instance of AudioPlayer:
*/
import 'package:audio_service/audio_service.dart';
import 'package:just_audio/just_audio.dart';
class AudioPlayerHandler extends BaseAudioHandler with ChangeNotifier {
final AudioPlayer _audioPlayer = AudioPlayerService.audioPlayer;
// Rest of the code...
}
Upvotes: 0
Reputation: 2766
This is likely a simple programming error with your app's state management which, for example, results in your creating a new player instance instead of reusing the same instance.
(Copied from my comment above since you confirmed it was the correct answer)
Upvotes: 1