避免Headset插件在iOS中停止AVAudioPlayer [英] avoid Headset plugout stops AVAudioPlayer in iOS

查看:94
本文介绍了避免Headset插件在iOS中停止AVAudioPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我使用AVAudioPlayer播放歌曲...但是当我在歌曲播放期间插入或插入耳机时,它会自动停止AVAudioPlayer ...我需要运行音频播放器,即使这些更改发生..任何想法都会受到赞赏。谢谢。

In my iPhone app I am using AVAudioPlayer to play the songs...But when I plug out or plugin the headset during song playing, It automatically stops the AVAudioPlayer... I need to run audio player even though these changes occur.. any ideas will be appreciated.Thanks in advance.

推荐答案

我找到了答案。

我们必须导入以下内容

#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVAudioPlayer.h>

并写下此代码

//Play the Event in Background
NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

现在它连续播放,即使插入耳机也是如此。

Now its continuously playing even I plugged in&out the Ear phone.

这篇关于避免Headset插件在iOS中停止AVAudioPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆