OGeek|极客世界-中国程序员成长平台

标题: ios - 关闭屏幕时 AVPlayer 停止播放音乐 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:51
标题: ios - 关闭屏幕时 AVPlayer 停止播放音乐

我的项目相关音乐和我在关闭屏幕时停止播放音乐的问题。虽然我注册了背景模式(音频),但有时当我关闭屏幕 AVPlayer 停止播放音乐并在打开屏幕时再次播放。

我花了很多时间,但我知道为什么会这样。



Best Answer-推荐答案


仅添加 audio 后台模式是不够的。您还必须设置播放 Audio Session 类别,如 described in Apple QA1668 :

A: You must declare your app plays audible content while in the background, and assign an appropriate category to your audio session.

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

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }

NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }

关于ios - 关闭屏幕时 AVPlayer 停止播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40083211/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4