iOS實(shí)現(xiàn)鎖屏頁面控制音樂播放
更新時間:2019年12月23日 09:06:52 作者:冰涼的枷鎖lax
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)鎖屏頁面控制音樂播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了iOS鎖屏頁面控制音樂播放的具體代碼,供大家參考,具體內(nèi)容如下
//1、調(diào)整音頻會話設(shè)置,確保應(yīng)用進(jìn)入后臺或靜音開關(guān)已開啟時音頻仍將繼續(xù)播放 //2、鎖屏狀態(tài)下顯示媒體信息 //3、鎖屏上的空間可以控制音頻播放 #import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController () @property (nonatomic, strong) AVAudioPlayer *audioPlayer; @property (weak, nonatomic) UIButton *playButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton * playButton = [UIButton buttonWithType:UIButtonTypeSystem]; playButton.frame = CGRectMake(0, 0, 200, 40); playButton.center = self.view.center; [playButton setTitle:@"在后臺播放音頻" forState:UIControlStateNormal]; [playButton addTarget:self action:@selector(playMusicInBackground:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:playButton]; NSError *playerInitError = nil; NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"background_audio" ofType:@"mp3"]; NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&playerInitError]; AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *activeError = nil; if (![session setActive:YES error:&activeError]) { NSLog(@"Failed to set active audio session!"); } //No.1 //開始寫代碼,調(diào)整音頻會話設(shè)置,確保即便應(yīng)用進(jìn)入后臺或靜音開關(guān)已開啟,音頻仍將繼續(xù)播放 NSError *categoryError = nil; [session setCategory:AVAudioSessionCategoryPlayback error:&categoryError]; //end_code } - (void)playMusicInBackground:(id)sender { if ([self.audioPlayer isPlaying]) { [self.audioPlayer stop]; [self.playButton setTitle:@"正在播放音樂" forState:UIControlStateNormal]; } else { UIImage *lockImage = [UIImage imageNamed:@"belongToMe.jpg"]; MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:lockImage]; NSDictionary *mediaDict = @{ MPMediaItemPropertyTitle: @"BackgroundTask Audio", MPMediaItemPropertyMediaType: @(MPMediaTypeAnyAudio), MPMediaItemPropertyPlaybackDuration: @(self.audioPlayer.duration), MPNowPlayingInfoPropertyPlaybackRate: @1.0, MPNowPlayingInfoPropertyElapsedPlaybackTime: @(self.audioPlayer.currentTime), MPMediaItemPropertyAlbumArtist: @"Some User", MPMediaItemPropertyArtist: @"Some User", MPMediaItemPropertyArtwork: artwork }; [self.audioPlayer play]; [self.playButton setTitle:@"停止播放后臺音樂" forState:UIControlStateNormal]; //No.2 //開始寫代碼,將媒體信息顯示在鎖定屏幕上,并使鎖屏上控件可以控制音頻播放 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; //end_code } } //No.3 //開始寫代碼,響應(yīng)遠(yuǎn)程控制,使得進(jìn)入鎖屏狀態(tài)后可以控制音樂“播放”和“暫?!? - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent { if (receivedEvent.type == UIEventTypeRemoteControl) { switch (receivedEvent.subtype) { case UIEventSubtypeRemoteControlPlay: [self.audioPlayer play]; break; case UIEventSubtypeRemoteControlPause: [self.audioPlayer pause]; break; default: NSLog(@"沒有處理過這個事件------receivedEvent.subtype==%ld",(long)receivedEvent.subtype); break; } } } //end_code - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS省市二級聯(lián)動的數(shù)據(jù)組織PHP版
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)之"省市"二級聯(lián)動的數(shù)據(jù)組織PHP版,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09關(guān)于iOS GangSDK的使用 為App快速集成社群公會模塊
這篇文章主要介紹了iOS GangSDK的使用為App快速集成社群公會模塊功能的實(shí)現(xiàn)過程。2017-11-11Flutter列表滾動定位超強(qiáng)輔助庫使用示例詳解
這篇文章主要為大家介紹了Flutter列表滾動定位超強(qiáng)輔助庫使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08iOS頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)
本文主要介紹了iOS頁面跳轉(zhuǎn)的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03iOS11 WKWebView內(nèi)容過濾規(guī)則詳解
本篇文章主要介紹了iOS11 WKWebView內(nèi)容過濾規(guī)則詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07