欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-18 22:27:00
本文实例为大家分享了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

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