iOS如何实现锁屏页面控制音乐播放-创新互联
小编给大家分享一下iOS如何实现锁屏页面控制音乐播放,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
成都创新互联公司是一家专注于网站设计制作、做网站与策划设计,吕梁网站建设哪家好?成都创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:吕梁等地区。吕梁做网站价格咨询:18980820575本文实例为大家分享了iOS锁屏页面控制音乐播放的具体代码,供大家参考,具体内容如下
//1、调整音频会话设置,确保应用进入后台或静音开关已开启时音频仍将继续播放 //2、锁屏状态下显示媒体信息 //3、锁屏上的空间可以控制音频播放 #import "ViewController.h" #import#import @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
看完了这篇文章,相信你对“iOS如何实现锁屏页面控制音乐播放”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联网站建设公司行业资讯频道,感谢各位的阅读!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前名称:iOS如何实现锁屏页面控制音乐播放-创新互联
文章出自:http://scgulin.cn/article/jhdod.html