OC-鎖屏播放

前言

我們?cè)趯慉PP中經(jīng)常會(huì)遇到需要鎖屏播放的這種情況,在這里,樓主就簡(jiǎn)單的說說鎖屏播放是怎么實(shí)現(xiàn)的。

此篇文章為OC版本,Swift版本請(qǐng)移步這里:Swift版本

如何實(shí)現(xiàn)

打開后臺(tái)模式

在設(shè)置的Capabilities中選擇后臺(tái)模式的第一項(xiàng),如下圖所示:


g

導(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];//總時(shí)長(zhǎng)
[info setObject:[NSNumber numberWithFloat:1.0] forKey:MPNowPlayingInfoPropertyPlaybackRate];//播放速率
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
}
打開后臺(tái)播放
#pragma mark 后臺(tái)播放
- (void)playingBackground
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
}

打開和關(guān)閉遠(yuǎn)程控制事件

viewWillAppear中打開接受遠(yuǎn)程控制事件,在viewWillDisappear中關(guān)閉遠(yuǎ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è)置鎖屏?xí)r,鎖屏界面的相關(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://切換播放暫停(耳機(jī)線控)
break;
case UIEventSubtypeRemoteControlNextTrack://下一首

break;
case UIEventSubtypeRemoteControlPreviousTrack://上一首

break;
case UIEventSubtypeRemoteControlBeginSeekingBackward://開始快退
break;
case UIEventSubtypeRemoteControlEndSeekingBackward://結(jié)束快退
break;
case UIEventSubtypeRemoteControlBeginSeekingForward://開始快進(jìn)
break;
case UIEventSubtypeRemoteControlEndSeekingForward://結(jié)束快進(jìn)
break;
default:
break;
}
}

到這里為止,鎖屏播放就完成了,Demo中附有完整的音樂播放器,Demo下載地址:Demo。最后,希望這篇文章對(duì)各位看官們有所幫助。對(duì)支持小編的看官們表示感謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容