音频播放器在后台播放,应该基于硬件静音开关工作

Audio Player play in background and should work based off of hardware mute switch

我想在前台、后台播放音频文件,它应该与静音开关一起工作,即如果静音开关打开,那么它不应该播放,如果静音开关关闭应该播放音频。

**我正在开发 SIP 呼叫应用程序。当用户接到电话时,应用程序应播放 sound/ringtone。如果应用程序在 background/foreground,它应该播放,如果硬件静音开关是 ON/OFF,它应该 mute/unmute。

为此,我使用了带有以下代码的 AVPlyaer。

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
[session setActive:YES error:nil];

NSURL * audioFileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test1" ofType:@"mp3"]];
AVPlayer *player = [[AVPlayer alloc] initWithURL: audioFileUrl];
[player play];

而且我在 info.plist

的背景模式中添加了 "App plays audio or streams audio/video using AirPlay"

这在两种模式下都在播放,但在硬件静音开关打开时不会静音。 如果我使用 AVAudioSessionCategoryAmbient 不在后台模式下播放。 我使用了 AVAudioPlayer 但在硬件开关打开时无法找到静音

您的音频会话类别可能设置不正确。 既然你想尊重静音开关,你应该使用 SoloAmbient

AVAudioSessionCategorySoloAmbient

The category for default category, used unless you set a category with the setCategory:error: or setCategory:withOptions:error: method.

Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

By default, using this category implies that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing, use the AVAudioSessionCategoryAmbient category instead.

Available in iOS 3.0 and later.

您似乎使用过此类别:

AVAudioSessionCategoryPlayback

The category for playing recorded music or other sounds that are central to the successful use of your app.

When using this category, your app audio continues with the Silent switch set to silent or when the screen locks. (The switch is called the Ring/Silent switch on iPhone.) To continue playing audio when your app transitions to the background (for example, when the screen locks), add the audio value to the UIBackgroundModes key in your information property list file.

By default, using this category implies that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing for this category, use the AVAudioSessionCategoryOptionMixWithOthers option.

Available in iOS 3.0 and later.

来源: https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html#//apple_ref/doc/constant_group/Audio_Session_Categories

这是一个用于检测静音开关状态的库:http://sharkfood.com/content/Developers/content/Sound%20Switch/。 它基于 Apple 的 Public API。它只是播放系统声音并测量完成播放所用的时间。它被用于 Instagram iOS 应用程序。

很高兴地说,我发现 Skype 如何实现此功能。

在前台我们可以使用AVAudioSession与任何一个类别来播放铃声。

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory: AVAudioSessionCategorySoloAmbient error:&error];
[session setActive:YES error:nil];

NSURL * audioFileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test1" ofType:@"mp3"]];
AVPlayer *player = [[AVPlayer alloc] initWithURL: audioFileUrl];
[player play];

在后台,我们必须使用带有自定义声音的本地通知。这将与硬件静音开关一起使用。但是这里的声音文件长度不能超过30秒

    UILocalNotification* notification = [[UILocalNotification alloc] init];
    [notification @"Title"];
    [notification setAlertBody:@"Body"];
    [notification setAlertAction:noteAction];
    [notification setSoundName:@"ringtone.mp3"];
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

希望这会有用:)。