ios – 音频播放器在后台播放,应该与硬件静音开关一起使用

ios – 音频播放器在后台播放,应该与硬件静音开关一起使用,第1张

概述我想在前台,后台播放音频文件,它应该与静音开关一起工作,即如果静音开关打开则不能播放,如果静音开关关闭则应播放音频. **基本上我正在开发SIP呼叫应用程序.当用户接到电话时,应该播放声音/铃声.如果app在后台/前台,它应该播放,如果硬件静音开关是ON / OFF,它应该静音/取消静音. 为此,我使用了AVPlyaer以下代码. AVAudioSession *session = [AVAudi 我想在前台,后台播放音频文件,它应该与静音开关一起工作,即如果静音开关打开则不能播放,如果静音开关关闭则应播放音频.

**基本上我正在开发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但在硬件开关打开时无法找到静音

请帮我修复它或任何其他方式来实现这一目标.
提前致谢.

解决方法 您的音频会话类别可能设置不正确.
由于您要尊重静音开关,因此您应该使用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

总结

以上是内存溢出为你收集整理的ios – 音频播放器在后台播放,应该与硬件静音开关一起使用全部内容,希望文章能够帮你解决ios – 音频播放器在后台播放,应该与硬件静音开关一起使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066917.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存