File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ public static class Audio
1010 {
1111 [ DllImport ( "__Internal" ) ] static extern void Audio_Init ( Action audioSessionRouteChangedCallback ,
1212 ULongCallback audioInterruptionCallback ) ;
13+ [ DllImport ( "__Internal" ) ] static extern bool Audio_SetActive ( bool active ) ;
14+ [ DllImport ( "__Internal" ) ] static extern bool Audio_GetAudioInterrupted ( ) ;
1315 [ DllImport ( "__Internal" ) ] static extern bool Audio_GetPrefersNoInterruptionsFromSystemAlerts ( ) ;
1416 [ DllImport ( "__Internal" ) ] static extern void Audio_SetPrefersNoInterruptionsFromSystemAlerts ( bool prefersNoInterruptions ) ;
1517 [ DllImport ( "__Internal" ) ] static extern float Audio_SystemVolume ( ) ;
@@ -28,6 +30,16 @@ static void Init()
2830 inited = true ;
2931 }
3032
33+ /// <summary>
34+ /// 设置AVAudioSession的激活状态
35+ /// </summary>
36+ /// <param name="active"></param>
37+ /// <returns>是否成功抢占音频优先权</returns>
38+ public static bool SetActive ( bool active )
39+ {
40+ return Audio_SetActive ( active ) ;
41+ }
42+
3143#region AudioInterruption
3244
3345 // https://developer.apple.com/documentation/avfaudio/avaudiosession/setprefersnointerruptionsfromsystemalerts(_:)?language=objc
@@ -49,6 +61,8 @@ public static bool PrefersNoInterruptionsFromSystemAlerts
4961
5062 static Action < AVAudioSessionInterruptionType > audioInterruptionEvent ;
5163
64+ public static bool AudioInterrupted => Audio_GetAudioInterrupted ( ) ;
65+
5266 /// <summary>
5367 /// 游戏音频中断与恢复事件
5468 /// </summary>
Original file line number Diff line number Diff line change 44
55+(void )Init : (Action)audioSessionRouteChangedCallback
66 audioInterruptionCallback : (ULongCallback)audioInterruptionCallback ;
7+ +(BOOL )SetActive : (BOOL )active ;
8+ +(bool )GetAudioInterrupted ;
79+(bool )GetPrefersNoInterruptionsFromSystemAlerts ;
810+(void )SetPrefersNoInterruptionsFromSystemAlerts : (BOOL )prefersNoInterruptions ;
911+(float )SystemVolume ;
@@ -23,6 +25,12 @@ extern "C"
2325 [Audio Init: audioSessionRouteChangedCallback
2426 audioInterruptionCallback: audioInterruptionCallback];
2527 }
28+ bool Audio_SetActive (bool active){
29+ return [Audio SetActive: active];
30+ }
31+ bool Audio_GetAudioInterrupted (){
32+ return [Audio GetAudioInterrupted ];
33+ }
2634 bool Audio_GetPrefersNoInterruptionsFromSystemAlerts (){
2735 return [Audio GetPrefersNoInterruptionsFromSystemAlerts ];
2836 }
Original file line number Diff line number Diff line change @@ -26,7 +26,19 @@ +(void)Init:(Action)audioSessionRouteChangedCallback
2626 inited = YES ;
2727}
2828
29- // static BOOL audioInterrupted;
29+ +(BOOL )SetActive : (BOOL )active
30+ {
31+ auto success = [[AVAudioSession sharedInstance ] setActive: active error: nil ];
32+ if (success)
33+ audioInterrupted = false ;
34+ return success;
35+ }
36+
37+ static BOOL audioInterrupted;
38+ +(bool )GetAudioInterrupted
39+ {
40+ return audioInterrupted;
41+ }
3042
3143+(bool )GetPrefersNoInterruptionsFromSystemAlerts
3244{
@@ -45,9 +57,8 @@ +(void)OnAudioInterruptionEvent:(NSNotification *)notification
4557{
4658 auto userInfo = notification.userInfo ;
4759 auto typeValue = [userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue ];
48- // auto type = (AVAudioSessionInterruptionType)typeValue;
49-
50- // audioInterrupted = type == AVAudioSessionInterruptionTypeBegan;
60+ auto type = (AVAudioSessionInterruptionType)typeValue;
61+ audioInterrupted = type == AVAudioSessionInterruptionTypeBegan;
5162
5263 /* switch (type) {
5364 case AVAudioSessionInterruptionTypeBegan:
You can’t perform that action at this time.
0 commit comments