Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause playback when playback device is unplugged #425

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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