iOS_33_音乐后台播放

音乐后台播放:三个步骤
在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];
}


其他三种常用的音频会话类型是:

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。