在AppDelegate.m中设置AVAudioSession类别 [英] Setting the AVAudioSession category in AppDelegate.m

查看:133
本文介绍了在AppDelegate.m中设置AVAudioSession类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我讨厌不得不问这个问题,但是我花了相当长的时间搜索Apple的文档和Google,但无济于事.我只是想在applicationDidFinishLaunching时为我的应用程序一次设置AVAudioSession类别.我有一个可播放音频流的应用程序,我希望它在进入背景时继续播放,因此我尝试使用播放"类别.这是我的AppDelegate.m代码:

So I hate to have to ask this question but I've spent a fair bit of time searching through Apple's documentation and Google with no avail. I'm simply trying to set the AVAudioSession category for my app ONCE, when the applicationDidFinishLaunching. I have an app that plays an audio stream and I would like it to continue playing when the app enters the background, so I'm trying to use the Playback category. Here is my code for AppDelegate.m :

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
[[AVAudioSession sharedInstance] setDelegate:self];

// create window and set up navigation controller
[window addSubview:myNavController.view];
[window makeKeyAndVisible];

}

# pragma mark -
# pragma mark AVAudioSession Delegate Methods 
- (void)beginInterruption {
}
- (void)endInterruption {
}
- (void)endInterruptionWithFlags:(NSUInteger)flags {
}
- (void)inputIsAvailableChanged:(BOOL)isInputAvailable {
}

使用此代码,每当我按下主屏幕按钮时,音频就会消失, 后台的应用程序.非常感谢您的帮助,我希望它能为您提供帮助 是任何以前做过此事的人的快速解决方案.

With this code, the audio fades out anytime I hit the home button, putting the app in the background. Any help is much appreciated, I hope that it is a quick fix type of answer for anybody who has done this before.

推荐答案

如果尚未完成,请首先将UIBackgroundModes键添加到Info.plist文件中. 更多信息此处.

First add the UIBackgroundModes key to your Info.plist file if you haven't done already. More info here.

如果您已经这样做了,您将使用哪个框架播放媒体?

If you have done that already, which framework do you use to play your media?

这篇关于在AppDelegate.m中设置AVAudioSession类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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