欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

iOS怎樣實現(xiàn)鎖屏頁面控制音樂播放

這篇文章將為大家詳細(xì)講解有關(guān)iOS怎樣實現(xiàn)鎖屏頁面控制音樂播放,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

10余年的通化網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整通化建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“通化網(wǎng)站設(shè)計”,“通化網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。

具體內(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)遠(yuǎ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

關(guān)于“iOS怎樣實現(xiàn)鎖屏頁面控制音樂播放”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

當(dāng)前文章:iOS怎樣實現(xiàn)鎖屏頁面控制音樂播放
本文地址:http://chinadenli.net/article38/pddcpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站策劃做網(wǎng)站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)