广告
返回顶部
首页 > 资讯 > 移动开发 >iOS实现锁屏页面控制音乐播放
  • 761
分享到

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

iOS锁屏音乐播放 2022-05-25 13:05:37 761人浏览 八月长安
摘要

本文实例为大家分享了iOS锁屏页面控制音乐播放的具体代码,供大家参考,具体内容如下 //1、调整音频会话设置,确保应用进入后台或静音开关已开启时音频仍将继续播放 //2、锁屏状

本文实例为大家分享了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: iOS实现锁屏页面控制音乐播放

本文链接: https://www.lsjlt.com/news/30343.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作