资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

iOS怎样实现锁屏页面控制音乐播放

这篇文章将为大家详细讲解有关iOS怎样实现锁屏页面控制音乐播放,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

10余年的通化网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整通化建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“通化网站设计”,“通化网站推广”以来,每个客户项目都认真落实执行。

具体内容如下

//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怎样实现锁屏页面控制音乐播放”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


文章名称:iOS怎样实现锁屏页面控制音乐播放
网页URL:http://cdkjz.cn/article/pddcpp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220