iOS實現(xiàn)鎖屏頁面控制音樂
本文實例為大家分享了iOS鎖屏頁面控制音樂 的具體代碼,供大家參考,具體內(nèi)容如下
//1、調(diào)整音頻會話設(shè)置,確保應(yīng)用進入后臺或靜音開關(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)用進入后臺或靜音開關(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)遠程控制,使得進入鎖屏狀態(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)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
欄 目:IOS
下一篇:iOS ScrollView嵌套tableView聯(lián)動滾動的思路與最佳實踐
本文標題:iOS實現(xiàn)鎖屏頁面控制音樂
本文地址:http://mengdiqiu.com.cn/a1/IOS/11855.html
您可能感興趣的文章
- 01-11iOS常用算法之兩個有序數(shù)組合并(要求時間復(fù)雜度為0(n))
- 01-11iOS 彈幕功能的實現(xiàn)思路圖解
- 01-11iOS調(diào)試Block引用對象無法被釋放的小技巧分享
- 01-11iOS動態(tài)更換Icon的全過程記錄
- 01-11iOS實現(xiàn)文本分頁的方法示例
- 01-11iOS常見宏理解及使用方法
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11iOS模擬中獎名單循環(huán)滾動效果
- 01-11Python一鍵查找iOS項目中未使用的圖片、音頻、視頻資源
- 01-11iOS中如何獲取某個視圖的截圖詳析


閱讀排行
本欄相關(guān)
- 01-11UILabel顯示定時器文本跳動問題的解決
- 01-11iOS常用算法之兩個有序數(shù)組合并(要
- 01-11iOS 彈幕功能的實現(xiàn)思路圖解
- 01-11詳解MacOs免密登錄CentOs操作步驟
- 01-11iOS動態(tài)更換Icon的全過程記錄
- 01-11iOS調(diào)試Block引用對象無法被釋放的小技
- 01-11iOS常見宏理解及使用方法
- 01-11iOS實現(xiàn)文本分頁的方法示例
- 01-11iOs遷至WKWebView跨過的一些坑
- 01-11iOS模擬中獎名單循環(huán)滾動效果
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結(jié)