SoFunction
Updated on 2025-04-06

Example code for music lock screen control music (lock screen information settings) in iOS

I won’t say much nonsense, I will just post the code to you. The specific code is as follows:

 <pre name="code" class="objc">appDelegateAdd the following code to obtain background playback permissions</pre><pre name="code" class="objc">- (void)setAudioBackstagePlay{ 
  AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
  [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; 
  [audioSession setActive:YES error:nil]; 
}</pre> 
<pre></pre> 
<pre name="code" class="objc">//Rewrite the parent class method and accept the processing of external events- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent { 
  NSLog(@"remote"); 
  if ( == UIEventTypeRemoteControl) { 
    switch () { // Get the event type      case UIEventSubtypeRemoteControlTogglePlayPause: // Pause ios6        [_player musicPause]; // Call the response method of the pause button of your project. The same is true for the following        break; 
      case UIEventSubtypeRemoteControlPreviousTrack: // Previous        [self lastMusic]; 
        break; 
      case UIEventSubtypeRemoteControlNextTrack: // Next        [self nextMusic]; 
        break; 
      case UIEventSubtypeRemoteControlPlay: //Play        [_player musicPlay]; 
        break; 
      case UIEventSubtypeRemoteControlPause: // Pause ios7        [_player musicPause]; 
        break; 
      default: 
        break; 
    } 
  } 
}</pre><pre name="code" class="objc"><pre name="code" class="objc">- (void)configNowPlayingCenter { 
  NSLog(@"Lock Screen Settings"); 
// BASE_INFO_FUN(@"Configure NowPlayingCenter");  YBVideoListModel * list = _model.audioList[_currentIndexPath.row];</pre><pre name="code" class="objc"><span style="white-space:pre">  </span>//The following code is to load the lock screen to display network pictures and other settings  [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:] options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 
    if (image == nil) { 
      image = [UIImage imageNamed:@"default_music"]; 
    } 
    NSMutableDictionary * info = [NSMutableDictionary dictionary]; 
    //The title of music    [info setObject: forKey:MPMediaItemPropertyTitle]; 
    //The artist of music    NSString *author= ; 
    [info setObject:author forKey:MPMediaItemPropertyArtist]; 
    //The music playback time    [info setObject:@(_player.) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; 
    //The playback speed of music    [info setObject:@(1) forKey:MPNowPlayingInfoPropertyPlaybackRate]; 
    //Total time of music    [info setObject:@() forKey:MPMediaItemPropertyPlaybackDuration]; 
    //The cover of the music    MPMediaItemArtwork * artwork = [[MPMediaItemArtwork alloc] initWithImage:image]; 
    [info setObject:artwork forKey:MPMediaItemPropertyArtwork]; 
    //Complete the settings    [[MPNowPlayingInfoCenter defaultCenter]setNowPlayingInfo:info]; 
  }]; 
}</pre><br> 
<br> 
<pre></pre> 
<br> 
</pre> 

The above is the example code for music lock screen control music (lock screen information settings) in iOS introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!