iOS on the Music Lock Screen Control Music of Lock Screen Information Setting Example Code
- 2021-10-25 08:04:50
- OfStack
No more nonsense, just post the code for everyone. The specific code is as follows:
<pre name="code" class="objc">appDelegate Add the following code to get background play permission </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">// Override the parent class method and accept the processing of external events
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
NSLog(@"remote");
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) { // Get the event type
case UIEventSubtypeRemoteControlTogglePlayPause: // Suspend ios6
[_player musicPause]; // Call the response method of the pause button of your project The same is true of the following
break;
case UIEventSubtypeRemoteControlPreviousTrack: // Upper 1 Head
[self lastMusic];
break;
case UIEventSubtypeRemoteControlNextTrack: // Under 1 Head
[self nextMusic];
break;
case UIEventSubtypeRemoteControlPlay: // Play
[_player musicPlay];
break;
case UIEventSubtypeRemoteControlPause: // Suspend ios7
[_player musicPause];
break;
default:
break;
}
}
}</pre><pre name="code" class="objc"><pre name="code" class="objc">- (void)configNowPlayingCenter {
NSLog(@" Lock screen setting ");
// 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:list.thumbUrl] 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 the music
[info setObject:list.title forKey:MPMediaItemPropertyTitle];
// An artist of music
NSString *author= list.singer;
[info setObject:author forKey:MPMediaItemPropertyArtist];
// The playing time of music
[info setObject:@(_player.player.currentTime.value) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
// The playing speed of music
[info setObject:@(1) forKey:MPNowPlayingInfoPropertyPlaybackRate];
// Total time of music
[info setObject:@(list.duration) forKey:MPMediaItemPropertyPlaybackDuration];
// The cover of music
MPMediaItemArtwork * artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
[info setObject:artwork forKey:MPMediaItemPropertyArtwork];
// Complete setup
[[MPNowPlayingInfoCenter defaultCenter]setNowPlayingInfo:info];
}];
}</pre><br>
<br>
<pre></pre>
<br>
</pre>