Change lock screen background audio controls text?
iOS 5 now supports setting the track title as well as an album art image in both the lock screen and the remote playback controls (the controls you get when you double-click the home button and swipe right). Take a look at theMPNowPlayingInfoCenter
class. Of course, to maximize compatibility, you'd want to test whether MPNowPlayingInfoCenter
is available, by doing something like:
if ([MPNowPlayingInfoCenter class]) {
/* we're on iOS 5, so set up the now playing center */
UIImage *albumArtImage = [UIImage imageNamed:@"HitchHikersGuide"];
albumArt = [[MPMediaItemArtwork alloc] initWithImage:albumArtImage];
NSDictionary *currentlyPlayingTrackInfo = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Life, the Universe and Everything", [NSNumber numberWithInt:42], albumArt, nil] forKeys:[NSArray arrayWithObjects:MPMediaItemPropertyTitle, MPMediaItemPropertyPlaybackDuration, MPMediaItemPropertyArtwork, nil]];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
}
AVPlayerLayer lock screen control
I got the solution simply used MPRemoteCommandCenter and added details about video see code in Objetive C
// to setup the playing info
- (void) setupNowPlayingInfoCenter{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.togglePlayPauseCommand setEnabled:YES];
[commandCenter.playCommand setEnabled:YES];
[commandCenter.pauseCommand setEnabled:YES];
[commandCenter.nextTrackCommand setEnabled:NO];
[commandCenter.previousTrackCommand setEnabled:NO];
[commandCenter.playCommand addTargetWithHandler: ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
[self->_player play];
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.pauseCommand addTargetWithHandler: ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
[self->_player pause];
return MPRemoteCommandHandlerStatusSuccess;
}];
}
// to update the playing info when enter in background
- (void) updateNowPlayingInfoCenter {
NSDictionary *metadata = [self.media metaData];
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
[songInfo setObject:metadata[MIBMediaMetaDataTrackNameKey] forKey:MPMediaItemPropertyTitle];
[songInfo setObject:metadata[MIBMediaMetaDataTrackNameKey] forKey:MPMediaItemPropertyArtist];
[songInfo setObject:metadata[MIBMediaMetaDataTrackDurationKey] forKey:MPMediaItemPropertyPlaybackDuration];
[songInfo setObject:[NSNumber numberWithDouble:(!self.playing ? 0.0f : 1.0f)] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[playingInfoCenter setNowPlayingInfo:songInfo];
}
// add this line of code in init or viewdidload
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
Called these 2 methods when enter in background
- (void)applicationDidEnterBackgroundNotification:(NSNotification *)notification {
[self setupNowPlayingInfoCenter];
[self updateNowPlayingInfoCenter];
}
Audio playback lock screen controls not displaying on iPhone
The issue turned out to be this line...
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers)
Once I changed it to
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
everything worked fine. So it seems that passing in any argument for AVAudioSessionCategoryPlaybackOptions causes the lock screen controls to not display. I also tried passing in .mixWithOthers
an that too caused the lock screen controls to not be displayed
MPRemoteCommandCenter - Remote controls on lock screen does not show up
Remove .mixWithOthers
from your category options.
I think the reasoning is that only the primary iOS audio app can control the remote screen. .mixWithOthers
is for secondary audio apps.
How Do I Get Audio Controls on Lock Screen/Control Center from AVAudioPlayer in Swift
You need to invoke beginReceivingRemoteControlEvents() otherwise it will not work on the actual device.
Swift 3.1
UIApplication.shared.beginReceivingRemoteControlEvents()
If you would like to specify custom actions for the MPRemoteCommandCenter:
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.addTarget(self, action:#selector(nextTrackCommandSelector))
edit/update:
Apple has a sample project showing how to Becoming a Now Playable App
How to allow user to control current audio duration from LockScreen or ControlCenter in iOS?
Have you gone through MPRemoteCommandCenter methods and properties, I believe you can use the commands here to skip forward or backword.
I am afraid you won't get a smooth slider similar to iTunes music as they have not exposed anything like that for developers.
And for setting track duration on lock screen you have to set MPMediaItemPropertyPlaybackDuration like below::
MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
NSDictionary *songInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:songDuration],MPMediaItemPropertyPlaybackDuration,
nil];
[center setNowPlayingInfo:songInfo];
Related Topics
Different Font Size for Different Devices in Xcode 6
iOS Swift Multiple Dimension Arrays - Compiliing Takes Ages. What Should I Change
Didreceiveremotenotification: Fetchcompletionhandler: Open from Icon VS Push Notification
How to Save Nsmutablearray in Nsuserdefaults
Swift Merge Audio and Video Files into One Video
Nsinteger and Nsuinteger in a Mixed 64Bit/32Bit Environment
Detecting Touches on Mkoverlay in iOS7 (Mkoverlayrenderer)
Repeating Local Notification Daily at a Set Time with Swift
How to Fix Failed to Fetch Default Token Error
How to Consecutively Present Two Alert Views Using Swiftui
Why Can't I Invert My Image Back to Original with Cifilter in My Swift iOS App
Is There a Public Way to Force Mpnowplayinginfocenter to Show Podcast Controls
What Tool(S) How to Use to Produce iPhone App Screencasts
Uploading Archive Error: "Missing iOS Distribution Signing Identity for ..."
How to Do Programmatically Gradient Border Color Uibutton with Swift