SoFunction
Updated on 2025-04-06

iOS implements lock screen page control music playback

This article shares the specific code for controlling music playback on iOS lock screen page for your reference. The specific content is as follows

//1. Adjust the audio session settings to ensure that the audio will continue to play when the application enters the background or the mute switch is turned on//2. Display media information in lock screen state//3. The space on the lock screen can control audio playback
#import ""
#import <AVFoundation/>
#import <MediaPlayer/>

@interface ViewController ()

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic) UIButton *playButton;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 UIButton * playButton = [UIButton buttonWithType:UIButtonTypeSystem];
  = CGRectMake(0, 0, 200, 40);
  = ;
 [playButton setTitle:@"Play audio in the background" forState:UIControlStateNormal];
 [playButton addTarget:self action:@selector(playMusicInBackground:) forControlEvents:UIControlEventTouchUpInside];
 [ addSubview:playButton];

 NSError *playerInitError = nil;

 NSString *audioPath =
 [[NSBundle mainBundle] pathForResource:@"background_audio"
         ofType:@"mp3"];

 NSURL *audioURL = [NSURL fileURLWithPath:audioPath];

  = [[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
 //Start write code, adjust audio session settings, and ensure that the audio will continue to play even if the application enters the background or the mute switch is turned on.
 NSError *categoryError = nil;
 [session setCategory:AVAudioSessionCategoryPlayback error:&categoryError];


 //end_code


}
- (void)playMusicInBackground:(id)sender {

 if ([ isPlaying]) {
  [ stop];

  [ setTitle:@"Playing music"
       forState:UIControlStateNormal];

 } else {
  UIImage *lockImage = [UIImage imageNamed:@""];

  MPMediaItemArtwork *artwork =
  [[MPMediaItemArtwork alloc] initWithImage:lockImage];

  NSDictionary *mediaDict =
  @{
   MPMediaItemPropertyTitle: @"BackgroundTask Audio",
   MPMediaItemPropertyMediaType: @(MPMediaTypeAnyAudio),
   MPMediaItemPropertyPlaybackDuration:
    @(),
   MPNowPlayingInfoPropertyPlaybackRate: @1.0,
   MPNowPlayingInfoPropertyElapsedPlaybackTime:
    @(),
   MPMediaItemPropertyAlbumArtist: @"Some User",
   MPMediaItemPropertyArtist: @"Some User",
   MPMediaItemPropertyArtwork: artwork };

  [ play];

  [ setTitle:@"Stop playing backstage music"
       forState:UIControlStateNormal];

  //No.2
  //Start write code, display media information on the lock screen, and enable controls on the lock screen to control audio playback

  [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];


  //end_code
 }
}

//No.3
//Start write code and respond to remote control, so that music can be controlled "play" and "pause" after entering the lock screen state.

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

 if ( == UIEventTypeRemoteControl) {
  switch () {

   case UIEventSubtypeRemoteControlPlay:
    [ play];
    break;

   case UIEventSubtypeRemoteControlPause:
    [ pause];
    break;

   default:
    NSLog(@"This event has not been handled------==%ld",(long));
    break;
  }
 }
}


//end_code

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
}

@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.