Command Center Scrubber on Lock Screen Swift

command center Scrubber on lock screen swift

I think your player property is an AVPlayer (???), and if so you want to use the seek function to set the currentTime, not set the return value from the function...

self.player.seek(to: CMTimeMakeWithSeconds(event.positionTime, 1000000))

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

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;
}

How can I make the Control Center slider editable?

There is the changePlaybackPositionCommand API with the associated Event MPChangePlaybackPositionCommandEvent.positionTime (see https://developer.apple.com/library/ios/releasenotes/General/iOS91APIDiffs/Objective-C/MediaPlayer.html)

I tried

[commandCenter.changePlaybackPositionCommand
addTarget: self
action: @selector( onChangePlaybackPositionCommand: )]

with the associated method

- (MPRemoteCommandHandlerStatus) onChangePlaybackPositionCommand:
(MPChangePlaybackPositionCommandEvent *) event
{
NSLog(@"changePlaybackPosition to %f", event.positionTime);
return MPRemoteCommandHandlerStatusSuccess;
}

but the cursor is still not movable and the method is not called. I guess I still miss something

Swift: command center not working, even though I explicitly set buttons to enable?

Maybe your question is old but someone could need this too, so this is how I did it :

Swift 5

let player = AVPlayer()

func commandCenter () {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in self.play(); return .success }
commandCenter.pauseCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in self.pause(); return .success }
}

func pause () {
player.pause ()
print("paused")
}

func play () {
player.play()
print("play")
}

You don't need to enable commandCenter.playCommand to true



Related Topics



Leave a reply



Submit