国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現鎖屏頁面控制音樂播放

2024-09-11 00:40:44
字體:
來源:轉載
供稿:網友

本文實例為大家分享了iOS鎖屏頁面控制音樂播放的具體代碼,供大家參考,具體內容如下

//1、調整音頻會話設置,確保應用進入后臺或靜音開關已開啟時音頻仍將繼續播放 //2、鎖屏狀態下顯示媒體信息 //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 //開始寫代碼,調整音頻會話設置,確保即便應用進入后臺或靜音開關已開啟,音頻仍將繼續播放 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//開始寫代碼,響應遠程控制,使得進入鎖屏狀態后可以控制音樂“播放”和“暫?!? (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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻江县| 容城县| 历史| 闽侯县| 惠东县| 内乡县| 关岭| 靖安县| 沧源| 固安县| 黔西| 岗巴县| 黔江区| 苍梧县| 旬邑县| 德江县| 奉化市| 贡嘎县| 依兰县| 车致| 扶风县| 万安县| 延边| 阜城县| 崇阳县| 阿图什市| 稻城县| 留坝县| 苍梧县| 黄石市| 胶州市| 宁陵县| 静乐县| 松桃| 临夏县| 内乡县| 高要市| 望城县| 浦北县| 分宜县| 仙桃市|