iOS音频的后台播放 锁屏
初始化AudioSession和基本配置
? ? ? 音頻播放器采用的AVPlayer ,在程序啟動的時候需要配置AudioSession,AudioSession負責應用音頻的設置,比如支不支持后臺,打斷等等,這一步很重要,比如在viewdidload里初始化AVplayer以后要調用下面的函數:
/** 設置音頻會話 */ ?//這種方式后臺,可以連續播放非網絡請求歌曲,遇到網絡請求歌曲就廢,需要后臺申請task
-(void)setAudioSession{
? ? AVAudioSession *audioSession=[AVAudioSession sharedInstance];
? ? [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
? ? [audioSession setActive:YES error:nil];
}
除了代碼的初始化,很重要的一步是對info-plist的設置,讓應用支持音頻的后臺播放
?
庫的引入包括:
AudioToolBox.framework
MediaPlayer.framework
CoreMedia.framework
AVFoundation.framework
?
Remote控制
在播放視圖的ViewController里加上這兩個函數:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
?
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
當然也可以同理放到delegate.m里面的進入后臺和回到前臺的函數中,否則的話,上面的代碼只是允許當前視圖的情況下進入后臺可以Remote控制
在AppDelegate里要申請后臺任務來進行處理
- (void)applicationDidEnterBackground:(UIApplication?*)application {
[application?beginReceivingRemoteControlEvents];
}
?
在添加遠程控制代碼:
?
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
?? ?
? ? //if it is a remote control event handle it correctly
?? ?
? ? if (event.type == UIEventTypeRemoteControl) {
?? ? ? ?
? ? ? ? if (event.subtype == UIEventSubtypeRemoteControlPlay) {
?? ? ? ? ? ?
? ? ? ? ? ? [self playBarSelector:self.mPlayButton];
?? ? ? ? ? ?
? ? ? ? }if (event.subtype == UIEventSubtypeRemoteControlPause) {
?? ? ? ? ? ?
? ? ? ? ? ? [self playBarSelector:self.mPlayButton];
?? ? ? ? ? ?
? ? ? ? } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack){
?? ? ? ? ? ?
? ? ? ? ? ? [self playBarSelector:self.mNextButton];
?? ? ? ? ?
? ? ? ? ? ? [self configNowPlayingInfoCenter];
?? ? ? ? ? ?
? ? ? ? }else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack){
?? ? ? ? ? ?
? ? ? ? ? ? [self playBarSelector:self.mUpwardButton];
? ? ? ? ? ?
?? ? ? ? ? ?
? ? ? ? ? ? [self configNowPlayingInfoCenter];
? ? ? ? }
? ? ?? ? }
}
?
最后切換上一首和下一首要更新鎖屏信息,重新調一下configNowPlayingInfoCenter方法
- (void)configNowPlayingInfoCenter {
?? ?
? ? if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
?? ? ? ?
? ? ? ? // 1.播放信息中心
? ? ? ? MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
?? ? ? ?
? ? ? ? // 2.初始化播放信息
? ? ? ? NSMutableDictionary *info = [NSMutableDictionary dictionary];
? ? ? ? // 專輯名稱
? ? ? ? info[MPMediaItemPropertyAlbumTitle] = self.operatorObject.mExerciseText;
? ? ? ? // 歌手
? ? ? ? info[MPMediaItemPropertyArtist] = @"雅思聽聽小組";
? ? ? ? // 歌曲名稱
? ? ? ? info[MPMediaItemPropertyTitle] = [NSString stringWithFormat:@"%@ - %@", self.operatorObject.mTextName, [self.operatorObject.mTitle substringToIndex:9]];
? ? ? ? // 設置圖片
? ? ? ? info[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"Default"]];
? ? ? ? // 設置持續時間(歌曲的總時間)
? ? ? ? info[MPMediaItemPropertyPlaybackDuration] = @(self.mAudioPlayerLong);
? ? ? ? // 設置當前播放進度
? ? ? ? info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(self.mPlayerCurrentTime);
?? ? ? ?
? ? ? ? // 3.切換播放信息
? ? ? ? center.nowPlayingInfo = info;
?
? ? }
?? ?
}
?
?
轉載于:https://www.cnblogs.com/Milo-CTO/p/4446919.html
總結
以上是生活随笔為你收集整理的iOS音频的后台播放 锁屏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nodejs mysql 创建连接池
- 下一篇: Tomcat tomcat-users.