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
Uiview.Animatewithduration Not Animating
Anchor Constraints Not Honored in Xcode 10/iOS 12
Swift - Setting Calayer Bounds or Frame Not Working
How to Delay a for Loop in Swift Without Interrupting The Main Thread
Querying Geohashes in Firestore Returns Nothing
Animate UIlabel Width with Fixed Center
Wrong Image Orientation After Displaytransform Call
Secidentity + Force Cast Violation: Force Casts Should Be Avoided. (Force_Cast)
Show Status Bar Only for iPhone X
Using a Timer in The Background Thread to Update UI
Can't Override UItableviewdatasource and UItableviewdelegate
Update UIapplicationshortcutitem from Extension
The "Funk" Sound When Hitting Escape Key in App
What Is The Reason to Store Subscription into a Subscriptions Set
Swiftui Go Back Programmatically from Representable to View