diff --git a/lib/player/states/player.dart b/lib/player/states/player.dart index bcf90d4d..ff55f22b 100644 --- a/lib/player/states/player.dart +++ b/lib/player/states/player.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:collection'; import 'package:audio_service/audio_service.dart'; +import 'package:audio_session/audio_session.dart'; import 'package:back_button_interceptor/back_button_interceptor.dart'; import 'package:bloc/bloc.dart'; import 'package:easy_debounce/easy_debounce.dart'; @@ -44,6 +45,7 @@ enum PlayerRepeat { noRepeat, repeatAll, repeatOne } class PlayerCubit extends Cubit with WidgetsBindingObserver { final SettingsCubit settings; late Orientation orientation; + late final AudioSession audioSession; PlayerCubit(super.initialState, this.settings) { orientation = _orientation; @@ -127,6 +129,16 @@ class PlayerCubit extends Cubit with WidgetsBindingObserver { rewindStep: settings.state.skipStep)); if (!isTv) { WidgetsBinding.instance.addObserver(this); + // setting up audio session + audioSession = await AudioSession.instance; + audioSession.configure(const AudioSessionConfiguration( + avAudioSessionCategory: AVAudioSessionCategory.playback, + avAudioSessionCategoryOptions: + AVAudioSessionCategoryOptions.allowBluetooth, + )); + audioSession.becomingNoisyEventStream.listen((event) { + pause(); + }); mediaHandler = await AudioService.init( builder: () => MediaHandler(this), @@ -231,7 +243,6 @@ class PlayerCubit extends Cubit with WidgetsBindingObserver { state: MediaState.playing, type: MediaEventType.miniDisplayChanged, value: state.isMini); - emit(state.copyWith(isClosing: true)); Future.delayed( animationDuration * 1.5, @@ -251,6 +262,8 @@ class PlayerCubit extends Cubit with WidgetsBindingObserver { setEvent(const MediaEvent(state: MediaState.idle)); }, ); + + audioSession.setActive(false); } double get getBottom => state.isHidden ? -targetHeight : 0; @@ -516,7 +529,7 @@ class PlayerCubit extends Cubit with WidgetsBindingObserver { _switchToVideo(IdedVideo video, {Duration? startAt}) async { try { // we move the existing video to the stack of played video - + await audioSession.setActive(true); bool isOffline = video is DownloadedVideo; // we want to switch to audio mode as soon as we can to prevent problems when switching from audio to video or the other way if (isOffline) { diff --git a/pubspec.lock b/pubspec.lock index d729c265..80381f85 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -74,7 +74,7 @@ packages: source: hosted version: "0.1.1" audio_session: - dependency: transitive + dependency: "direct main" description: name: audio_session sha256: "6fdf255ed3af86535c96452c33ecff1245990bb25a605bfb1958661ccc3d467f" diff --git a/pubspec.yaml b/pubspec.yaml index 5e4691f5..0cd72774 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -83,6 +83,7 @@ dependencies: workmanager: 0.5.2 flutter_displaymode: 0.6.0 freezed_annotation: 2.4.1 + audio_session: 0.1.18 dependency_overrides: # wakelock_windows: