Skip to content

Commit 5dce3d9

Browse files
committed
增加Audio.PrefersNoInterruptionsFromSystemAlerts
1 parent f363790 commit 5dce3d9

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

Plugins/CS/Audio.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff 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_GetPrefersNoInterruptionsFromSystemAlerts();
14+
[DllImport("__Internal")] static extern void Audio_SetPrefersNoInterruptionsFromSystemAlerts(bool prefersNoInterruptions);
1315
[DllImport("__Internal")] static extern float Audio_SystemVolume();
1416
[DllImport("__Internal")] static extern double Audio_InputLatency();
1517
[DllImport("__Internal")] static extern double Audio_OutputLatency();
@@ -28,10 +30,27 @@ static void Init()
2830

2931
#region AudioInterruption
3032

33+
// https://developer.apple.com/documentation/avfaudio/avaudiosession/setprefersnointerruptionsfromsystemalerts(_:)?language=objc
34+
35+
/// <summary>
36+
/// <para>设置系统铃声时是否中断Audio Session</para>
37+
/// <para>Beginning in iOS 14, users can set a global preference that indicates whether the system displays incoming calls using a banner or a full-screen display style.
38+
/// If using the banner style, setting this value to true prevents the system from interrupting the audio session with incoming call notifications,
39+
/// and gives the user an opportunity to accept or decline the call. The system only interrupts the audio session if the user accepts the call.</para>
40+
/// <para>Enabling this preference can improve the user experience of apps with audio sessions that
41+
/// you don’t want to interrupt, such as those that record audiovisual media or that you use for music performance.</para>
42+
/// <para>This preference has no effect if the device uses the full-screen display style—the system interrupts the audio session on incoming calls.</para>
43+
/// </summary>
44+
public static bool PrefersNoInterruptionsFromSystemAlerts
45+
{
46+
get => Audio_GetPrefersNoInterruptionsFromSystemAlerts();
47+
set => Audio_SetPrefersNoInterruptionsFromSystemAlerts(value);
48+
}
49+
3150
static Action<AVAudioSessionInterruptionType> audioInterruptionEvent;
3251

3352
/// <summary>
34-
/// 游戏音频干扰与恢复事件
53+
/// 游戏音频中断与恢复事件
3554
/// </summary>
3655
public static event Action<AVAudioSessionInterruptionType> AudioInterruptionEvent
3756
{

Plugins/Native/Headers/Audio.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
@interface Audio : NSObject
44

55
+(void)Init:(Action)audioSessionRouteChangedCallback
6-
audioInterruptionEvent:(ULongCallback)audioInterruptionCallback;
6+
audioInterruptionCallback:(ULongCallback)audioInterruptionCallback;
7+
+(bool)GetPrefersNoInterruptionsFromSystemAlerts;
8+
+(void)SetPrefersNoInterruptionsFromSystemAlerts:(BOOL)prefersNoInterruptions;
79
+(float)SystemVolume;
810
+(double)InputLatency;
911
+(double)OutputLatency;
@@ -19,7 +21,13 @@ extern "C"
1921
ULongCallback audioInterruptionCallback)
2022
{
2123
[Audio Init:audioSessionRouteChangedCallback
22-
audioInterruptionEvent:audioInterruptionCallback];
24+
audioInterruptionCallback:audioInterruptionCallback];
25+
}
26+
bool Audio_GetPrefersNoInterruptionsFromSystemAlerts(){
27+
return [Audio GetPrefersNoInterruptionsFromSystemAlerts];
28+
}
29+
void Audio_SetPrefersNoInterruptionsFromSystemAlerts(bool prefersNoInterruptions){
30+
[Audio SetPrefersNoInterruptionsFromSystemAlerts:prefersNoInterruptions];
2331
}
2432
float Audio_SystemVolume()
2533
{

Plugins/Native/Implementations/Audio.mm

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ +(void)Init:(Action)audioSessionRouteChangedCallback
2828

2929
//static BOOL audioInterrupted;
3030

31+
+(bool)GetPrefersNoInterruptionsFromSystemAlerts
32+
{
33+
if (@available(iOS 14.5, *))
34+
return [[AVAudioSession sharedInstance] prefersNoInterruptionsFromSystemAlerts];
35+
return false;
36+
}
37+
38+
+(void)SetPrefersNoInterruptionsFromSystemAlerts:(BOOL)prefersNoInterruptions
39+
{
40+
if (@available(iOS 14.5, *))
41+
[[AVAudioSession sharedInstance] setPrefersNoInterruptionsFromSystemAlerts:prefersNoInterruptions error:nil];
42+
}
3143

3244
+(void)OnAudioInterruptionEvent:(NSNotification *)notification
3345
{
3446
auto userInfo = notification.userInfo;
3547
auto typeValue = [userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
36-
auto type = (AVAudioSessionInterruptionType)typeValue;
48+
//auto type = (AVAudioSessionInterruptionType)typeValue;
3749

3850
//audioInterrupted = type == AVAudioSessionInterruptionTypeBegan;
3951

0 commit comments

Comments
 (0)