前言
我們在寫APP中經(jīng)常會遇到需要鎖屏播放的這種情況精偿,在這里,樓主就簡單的說說鎖屏播放是怎么實現(xiàn)的。
此篇文章為OC版本扎阶,Swift版本請移步這里:Swift版本
如何實現(xiàn)
打開后臺模式
在設(shè)置的Capabilities中選擇后臺模式的第一項洛姑,如下圖所示:
導(dǎo)入相應(yīng)的頭文件
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
播放音樂并設(shè)置相關(guān)信息
播放音樂并設(shè)置鎖屏需要顯示的相關(guān)信息
#pragma mark 播放音樂
- (void)playMusic
{
self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:self.model.filename withExtension:nil] error:nil];
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
if (!self.audioPlayer.isPlaying) {
[self.audioPlayer play];
}
}
#pragma mark 設(shè)置鎖屏信息顯示
- (void)setLockScreenDisplay
{
NSMutableDictionary *info = [NSMutableDictionary dictionary];
[info setObject:self.model.name forKey:MPMediaItemPropertyTitle];//歌名
[info setObject:self.model.singer forKey:MPMediaItemPropertyArtist];//作者
// [info setObject:self.model.filename forKey:MPMediaItemPropertyAlbumTitle];//專輯名
[info setObject:self.model.singer forKey:MPMediaItemPropertyAlbumArtist];//專輯作者
[info setObject:[[MPMediaItemArtwork alloc]initWithImage:[UIImage imageNamed:self.model.icon]] forKey:MPMediaItemPropertyArtwork];//顯示的圖片
[info setObject:[NSNumber numberWithDouble:self.audioPlayer.duration] forKey:MPMediaItemPropertyPlaybackDuration];//總時長
[info setObject:[NSNumber numberWithFloat:1.0] forKey:MPNowPlayingInfoPropertyPlaybackRate];//播放速率
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
}
打開后臺播放
#pragma mark 后臺播放
- (void)playingBackground
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
}
打開和關(guān)閉遠程控制事件
在viewWillAppear中打開接受遠程控制事件,在viewWillDisappear中關(guān)閉遠程控制事件,代碼如下:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self becomeFirstResponder];
[[UIApplication sharedApplication]beginReceivingRemoteControlEvents];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self resignFirstResponder];
[[UIApplication sharedApplication]endReceivingRemoteControlEvents];
}
設(shè)置鎖屏控制
設(shè)置鎖屏時,鎖屏界面的相關(guān)控制按鈕所應(yīng)該執(zhí)行的方法
#pragma mark 鎖屏控制
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay://播放
break;
case UIEventSubtypeRemoteControlPause://暫停
break;
case UIEventSubtypeRemoteControlStop://停止
break;
case UIEventSubtypeRemoteControlTogglePlayPause://切換播放暫停(耳機線控)
break;
case UIEventSubtypeRemoteControlNextTrack://下一首
break;
case UIEventSubtypeRemoteControlPreviousTrack://上一首
break;
case UIEventSubtypeRemoteControlBeginSeekingBackward://開始快退
break;
case UIEventSubtypeRemoteControlEndSeekingBackward://結(jié)束快退
break;
case UIEventSubtypeRemoteControlBeginSeekingForward://開始快進
break;
case UIEventSubtypeRemoteControlEndSeekingForward://結(jié)束快進
break;
default:
break;
}
}
到這里為止上沐,鎖屏播放就完成了,Demo中附有完整的音樂播放器楞艾,Demo下載地址:Demo参咙。最后,希望這篇文章對各位看官們有所幫助硫眯。對支持小編的看官們表示感謝蕴侧。