Skip to content

Commit

Permalink
Merge pull request #425 from lamarios/feature/stop_playback_on_headph…
Browse files Browse the repository at this point in the history
…one_unplug

Pause playback when playback device is unplugged
  • Loading branch information
lamarios authored Dec 20, 2023
2 parents acd45be + 4434177 commit d678a80
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/player/states/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -44,6 +45,7 @@ enum PlayerRepeat { noRepeat, repeatAll, repeatOne }
class PlayerCubit extends Cubit<PlayerState> with WidgetsBindingObserver {
final SettingsCubit settings;
late Orientation orientation;
late final AudioSession audioSession;

PlayerCubit(super.initialState, this.settings) {
orientation = _orientation;
Expand Down Expand Up @@ -127,6 +129,16 @@ class PlayerCubit extends Cubit<PlayerState> 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),
Expand Down Expand Up @@ -231,7 +243,6 @@ class PlayerCubit extends Cubit<PlayerState> with WidgetsBindingObserver {
state: MediaState.playing,
type: MediaEventType.miniDisplayChanged,
value: state.isMini);

emit(state.copyWith(isClosing: true));
Future.delayed(
animationDuration * 1.5,
Expand All @@ -251,6 +262,8 @@ class PlayerCubit extends Cubit<PlayerState> with WidgetsBindingObserver {
setEvent(const MediaEvent(state: MediaState.idle));
},
);

audioSession.setActive(false);
}

double get getBottom => state.isHidden ? -targetHeight : 0;
Expand Down Expand Up @@ -516,7 +529,7 @@ class PlayerCubit extends Cubit<PlayerState> 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) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ packages:
source: hosted
version: "0.1.1"
audio_session:
dependency: transitive
dependency: "direct main"
description:
name: audio_session
sha256: "6fdf255ed3af86535c96452c33ecff1245990bb25a605bfb1958661ccc3d467f"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d678a80

Please sign in to comment.