@@ -3,6 +3,9 @@ import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
3
3
import 'package:catcher_2/catcher_2.dart' ;
4
4
import 'package:collection/collection.dart' ;
5
5
import 'package:media_kit/media_kit.dart' ;
6
+ import 'package:flutter_broadcasts/flutter_broadcasts.dart' ;
7
+ import 'package:package_info_plus/package_info_plus.dart' ;
8
+ import 'package:audio_session/audio_session.dart' ;
6
9
// ignore: implementation_imports
7
10
import 'package:spotube/services/audio_player/playback_state.dart' ;
8
11
@@ -14,13 +17,23 @@ class MkPlayerWithState extends Player {
14
17
final StreamController <bool > _shuffleStream;
15
18
final StreamController <PlaylistMode > _loopModeStream;
16
19
20
+ static const String EXTRA_PACKAGE_NAME = "android.media.extra.PACKAGE_NAME" ;
21
+ static const String EXTRA_AUDIO_SESSION = "android.media.extra.AUDIO_SESSION" ;
22
+ static const String ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION =
23
+ "android.media.action.OPEN_AUDIO_EFFECT_CONTROL_SESSION" ;
24
+ static const String ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION =
25
+ "android.media.action.CLOSE_AUDIO_EFFECT_CONTROL_SESSION" ;
26
+
17
27
late final List <StreamSubscription > _subscriptions;
18
28
19
29
bool _shuffled;
20
30
PlaylistMode _loopMode;
21
31
22
32
Playlist ? _playlist;
23
33
List <Media >? _tempMedias;
34
+ int _androidAudioSessionId = 0 ;
35
+ String _packageName = "" ;
36
+ AndroidAudioManager ? _androidAudioManager;
24
37
25
38
MkPlayerWithState ({super .configuration})
26
39
: _playerStateStream = StreamController .broadcast (),
@@ -64,11 +77,33 @@ class MkPlayerWithState extends Player {
64
77
Catcher2 .reportCheckedError ('[MediaKitError] \n $event ' , null );
65
78
}),
66
79
];
67
- if (super .platform is NativePlayer ) {
68
- NativePlayer playerNative = super .platform as NativePlayer ;
69
- if (DesktopTools .platform.isAndroid) {
70
- playerNative.setProperty ("ao" , "audiotrack,opensles" );
71
- }
80
+ PackageInfo .fromPlatform ().then ((packageInfo) {
81
+ _packageName = packageInfo.packageName;
82
+ });
83
+ if (DesktopTools .platform.isAndroid) {
84
+ _androidAudioManager = AndroidAudioManager ();
85
+ AudioSession .instance.then ((s) async {
86
+ _androidAudioSessionId =
87
+ await _androidAudioManager! .generateAudioSessionId ();
88
+ notifyAudioSessionUpdate (true );
89
+
90
+ nativePlayer.setProperty (
91
+ "audiotrack-session-id" , _androidAudioSessionId.toString ());
92
+ nativePlayer.setProperty ("ao" , "audiotrack,opensles," );
93
+ });
94
+ }
95
+ }
96
+
97
+ Future <void > notifyAudioSessionUpdate (bool active) async {
98
+ if (DesktopTools .platform.isAndroid) {
99
+ sendBroadcast (BroadcastMessage (
100
+ name: active
101
+ ? ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION
102
+ : ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION ,
103
+ data: {
104
+ EXTRA_AUDIO_SESSION : _androidAudioSessionId,
105
+ EXTRA_PACKAGE_NAME : _packageName
106
+ }));
72
107
}
73
108
}
74
109
@@ -146,10 +181,11 @@ class MkPlayerWithState extends Player {
146
181
}
147
182
148
183
@override
149
- Future <void > dispose () {
184
+ Future <void > dispose () async {
150
185
for (var element in _subscriptions) {
151
186
element.cancel ();
152
187
}
188
+ await notifyAudioSessionUpdate (false );
153
189
return super .dispose ();
154
190
}
155
191
0 commit comments