我想在前台,后台播放音频文件,它应该与静音开关一起工作,即如果静音开关打开则不能播放,如果静音开关关闭则应播放音频 .
**基本上我正在开发SIP呼叫应用程序 . 当用户接到电话时,应该播放声音/铃声 . 它应该在app处于后台/前台时播放,如果硬件静音开关为ON / OFF,它应该静音/取消静音 .
为此,我使用了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播放音频或使用AirPlay传输音频/视频”到后台模式
这是在两种模式下播放,但在硬件静音开关打开时不静音 . 如果我使用“AVAudioSessionCategoryAmbient”不在后台模式上播放 . 我使用AVAudioPlayer但在硬件开关打开时无法找到静音
请帮我修复它或任何其他方法来实现这一目标 . 提前致谢 .
3 回答
您的音频会话类别可能设置不正确 . 由于您要尊重静音开关,因此您应该使用SoloAmbient
您似乎已使用此类别:
Source: https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html#//apple_ref/doc/constant_group/Audio_Session_Categories
很高兴地说,我发现skype是如何实现这一功能的 .
在前台,我们可以使用任何一个类别的AVAudioSession来播放铃声 .
在后台,我们必须使用自定义声音的本地通知 . 这将适用于硬件静音开关 . 但这里的声音文件长度不应超过30秒 .
希望这将是有用的:) .
这是一个用于检测静音开关状态的库:http://sharkfood.com/content/Developers/content/Sound%20Switch/ . 它基于Apple的公共API . 它只播放系统声音并测量用于完成播放的时间量 . 它用于Instagram iOS应用程序 .