How to change track position on lock screen/control center?
Asked Answered
G

3

13

When playing a song with the ios 7 music app the user can use slider to change song position in the lock screen/the control center. Slider is active:

enter image description here

But when playing music in my app user can't do it. Slider isn't active:

enter image description here

How can i enable these feature in my app?

Gangue answered 25/10, 2013 at 13:10 Comment(3)
How are you playing music in your app?Chlamydeous
@Chlamydeous via MPMoviePlayerControllerGangue
@NeimanAleksei how do you show the song title and song duration ?Hubbub
G
13

You can change track position with help of MPRemoteCommandCenter on iOS 9.1 and higher.

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_0) {
            MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
            [commandCenter.changePlaybackPositionCommand setEnabled:true];
            [commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)];
        }

and method

- (MPRemoteCommandHandlerStatus)changedThumbSliderOnLockScreen:(MPChangePlaybackPositionCommandEvent *)event
{
    // change position
    [self setCurrentPlaybackTime:event.positionTime];
    // update MPNowPlayingInfoPropertyElapsedPlaybackTime
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

    return MPRemoteCommandHandlerStatusSuccess;
}
Gangue answered 16/10, 2016 at 21:52 Comment(0)
A
11

swift4 You can change track position with help of MPRemoteCommandCenter on iOS 9.1 and higher.

let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.changePlaybackPositionCommand.isEnabled = true
commandCenter.changePlaybackPositionCommand.addTarget(
 self, action:#selector(changePlaybackPositionCommand(_:)))

and method

@objc func changePlaybackPositionCommand(_ event:
          MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus {
    let time = event.positionTime
    //use time to update your track time
    return MPRemoteCommandHandlerStatus.success
}

Note that you have to do that for every command in commandCenter if you want that commend to be enabled.

Ales answered 8/1, 2018 at 9:27 Comment(0)
O
5

I was looking for the same thing but I don't think this is possible see this post:

How to enable audio scrubber in iOS Lock Screen control panel?

Also popular apps like Spotify and Soundcloud don't have this implemented.

If you are looking for a way to show the current music on the lock screen you need to do the following.

First when you play a new track update the NowPlayingInfo :

NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

    [songInfo setObject:trackTitle forKey:MPMediaItemPropertyTitle];
    [songInfo setObject:artistName forKey:MPMediaItemPropertyArtist];
    [songInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
    [songInfo setObject:releaseDate forKey:MPMediaItemPropertyReleaseDate];
    [songInfo setValue:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
    [songInfo setObject:elapsedTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
    [songInfo setObject:albumArtImage forKey:MPMediaItemPropertyArtwork];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

To handle events from the Lockscreen, you first need to tell your app to start receiving events from the remote control. I do this in the application didFinishLaunchingWithOptions of my AppDelegate using the following code

 // Turn on remote control event delivery
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Next you need to implement remoteControlReceivedWithEvent method to handle the captured events. In APPDelegate add the following method

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

 if (receivedEvent.type == UIEventTypeRemoteControl) {

    switch (receivedEvent.subtype) {
        case UIEventSubtypeRemoteControlPause:
            //pause code here
            break;

        case UIEventSubtypeRemoteControlPlay:
             //play code here
            break;

        case UIEventSubtypeRemoteControlPreviousTrack:
            // previous track code here
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            //next track code here
            break;

        default:
            break;
    }
 }

}

More info on MPNowPlayingInfoCenter from the apple docs -> https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPNowPlayingInfoCenter_Class

Olivo answered 17/1, 2014 at 9:31 Comment(2)
How can I use remoteControlReceivedWithEvent ?Hierarchy
@KrunalNagvadia in swift : override func remoteControlReceived(with event: UIEvent?) { switch event?.subtype { case .remoteControlPlay: print("") default: break } }Ashlaring

© 2022 - 2024 — McMap. All rights reserved.