音乐后台播放:三个步骤
在appDelegate中的didEnterBackground方法中实现:
app的beginBackgroundTaskWithExpirationHandler方法
开启后台任务,让程序在后台运行
- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. // 后台播放三步骤之一:让应用在后台运行 [application beginBackgroundTaskWithExpirationHandler:nil]; }
在Supporting Files目录下的【33_音效-Info.plist】
增加一项【Required background modes】
值为【App plays audio or streams audio/video using AirPlay】
第三、在工具类的初始化方法【
initialize】中,设置音频会话的类型,并激活
+ (void)initialize { // 后台播放,第三步,设置 音频会话类型 AVAudioSession *session = [AVAudioSession sharedInstance]; // 类型是:播放和录音 [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; // 而且要激活 音频会话 [session setActive:YES error:nil]; }
其他三种常用的音频会话类型是:
时间: 2024-10-21 21:51:09