I am using audio_service with just_audio in my app.when the app starts , they player is playing fine and the background also working fine.but when I enter to the app again(resuming the app).the player widget in the notification panel disappear and the background play stopped working. what may be the issue ?
In console I am getting [MRNowPlaying] Ignoring setPlaybackState because application does not contain entitlement com.apple.mediaremote.set-playback-state for platform and I am using FlutterVolumeController package as well.
My DI section is
setUpLocators() async{
// common
sl.registerSingletonAsync<AudioHandler>(() async => await initAudioService());
sl.registerLazySingleton<ThemeBloc>(() => ThemeBloc());
sl.registerLazySingleton<AuthBloc>(() => AuthBloc(
useCases: sl<AuthUseCases>(),
));
Main method is
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
enableCrashlytics();
/// setting the environment for production or staging.
setEnv(EnvironmentType.production);
setUpLocators();
Bloc.observer = AppBlocObserver();
setUpThemeChangeObserver();
runApp(const MyApp());
}
AudioHandler class is
Future<AudioHandler> initAudioService() async {
return await AudioService.init(
builder: () => MyAudioHandler(),
config: const AudioServiceConfig(
androidNotificationChannelId: 'io.exmple.audio',
androidNotificationChannelName: 'name1',
androidNotificationOngoing: true,
androidStopForegroundOnPause: true,
),
);
}
class MyAudioHandler extends BaseAudioHandler {
final AudioPlayer _player = AudioPlayer();
MyAudioHandler() {
_player.playbackEventStream.map(_transformEvent).pipe(playbackState);
}
@override
Future<void> playFromUri(Uri uri, [Map<String, dynamic>? extras]) async {
mediaItem.add(MediaItem(id: uri.path.toString(), title: 'Sample'));
_player.setAudioSource(
HlsAudioSource(
uri,
),
);
return;
}
@override
Future<void> play() async {
await _player.play();
return super.play();
}
@override
Future<void> pause() async {
await _player.pause();
return super.pause();
}
@override
Future<void> stop() async {
await _player.stop();
return super.stop();
}
PlaybackState _transformEvent(PlaybackEvent event) {
return PlaybackState(
controls: [
if (_player.playing) MediaControl.pause else MediaControl.play,
],
androidCompactActionIndices: const [0, 1, 3],
processingState: const {
ProcessingState.idle: AudioProcessingState.idle,
ProcessingState.loading: AudioProcessingState.loading,
ProcessingState.buffering: AudioProcessingState.buffering,
ProcessingState.ready: AudioProcessingState.ready,
ProcessingState.completed: AudioProcessingState.completed,
}[_player.processingState]!,
playing: _player.playing,
updatePosition: _player.position,
bufferedPosition: _player.bufferedPosition,
speed: _player.speed,
queueIndex: event.currentIndex,
);
}
@override
Future<void> click([MediaButton button = MediaButton.media]) async {
print('button===${button}');
switch (button) {
case MediaButton.media:
{
if (playbackState.valueOrNull?.playing == true) {
await _player.pause();
} else {
await _player.play();
}
break;
}
case MediaButton.next:
// TODO: Handle this case.
case MediaButton.previous:
// TODO: Handle this case.
}
return super.click(button);
}
}
audioHandler is using in a bloc class as follows.
AudioHandler playerHandler = sl<AudioHandler>();
await playerHandler.playFromUri(Uri.parse(
'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8'
));
I have created a sample app with same code for audio service (only having dependencies audio_service and just_audio ) and it works fine for the background.I am not sure that any other dependencies in my project causing the issue. Please help me out.
I was using flutter_volume_controller package and it was causing the problem.I replaced the package with volume_controller and it is working perfectly fine.