iPhone应用程序允许继续播放背景音乐 [英] iphone app allow background music to continue to play

查看:103
本文介绍了iPhone应用程序允许继续播放背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在声音播放时启动我的iPhone游戏时,播放的背景音乐或播客停止播放。我注意到其他游戏允许继续播放背景音频。

When I launch my iPhone game as soon as a sound plays the background music or podcast that is playing stops. I noticed other games allow background audio to continue to play.

这怎么可能?我是否需要覆盖App Delegate中的方法?

How is this possible? Do I need to override a method in my App Delegate?

推荐答案

将此行放在应用程序中: didFinishLaunchingWithOptions:你的 AppDelegate 的方法,或者一般在使用音频播放器之前。

Place this line in your application:didFinishLaunchingWithOptions: method of your AppDelegate or in general before using the audio player.

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

根据文档, AVAudioSessionCategoryAmbient 类别


对于声音播放非主要的应用程序 - 也就是说,您的应用程序可以在关闭声音的情况下成功使用。

for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.

此类别也适用于播放风格的应用程序,例如用户在iPod音频上播放的虚拟钢琴。 当您使用此类别时,来自其他应用程序的音频会与您的音频混合。您的音频会被屏幕锁定和静音开关(在iPhone上称为响铃/静音开关)静音。

This category is also appropriate for "play along" style apps, such as a virtual piano that a user plays over iPod audio. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

如果你还想确保没有错误发生,你必须检查返回值

If you want also to ensure that no error occurred you have to check the return value

NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (!success) {
     //Handle error
     NSLog(@"%@", [error localizedDescription]);
} else {
   // Yay! It worked!      
}

作为最后的评论,请不要忘记链接 AVFoundation 框架到你的项目并导入它。

As a final remark, don't forget to link the AVFoundation framework to your project and import it.

#import <AVFoundation/AVFoundation.h>

这篇关于iPhone应用程序允许继续播放背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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