在AppDelegate.m中设置AVAudioSession类别
所以我不想问这个问题,但我花了相当多的时间搜索苹果的文档和谷歌,但没有结果。我只是想在 applicationDidFinishLaunching 时为我的应用程序设置一次 AVAudioSession 类别。我有一个播放音频流的应用程序,我希望它在应用程序进入后台时继续播放,因此我尝试使用“播放”类别。这是我的 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 {
}
使用此代码,每当我按下主页按钮时,音频都会淡出,将 该应用程序在后台运行。非常感谢任何帮助,我希望它 对于以前做过此操作的人来说,这是一个快速修复类型的答案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先将
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?
感谢艾琳的帮助。您的答案非常正确,只是我只想提供它对我有用所需的步骤。我阅读了您发布的苹果文档,由于某种原因,它遗漏了这些重要的细节:
当然,您的应用程序还应该结合设置此键来设置其音频会话类别。
Thanks for the help Irene. You are pretty much right with your answer except I just wanted to provide the steps that were necessary for it to work for me. I read the apple documentation that you posted and for some reason it left these important details out:
Of course your app should also take care of setting its audio session category in combination with setting this key.