gpt4 book ai didi

iphone app允许背景音乐继续播放

转载 作者:IT王子 更新时间:2023-10-29 07:56:59 26 4
gpt4 key购买 nike

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

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

最佳答案

将此行放在 application:didFinishLaunchingWithOptions:AppDelegate 方法中,或者通常在使用音频播放器之前。

 [[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.

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).

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

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

最后一点,不要忘记将 AVFoundation 框架链接到您的项目并导入它。

#import <AVFoundation/AVFoundation.h>

关于iphone app允许背景音乐继续播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14122363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com