我正在开发一个可播放声音文件的应用程序.如果我打开苹果音乐应用程序,滑块让我在我所在的歌曲之间移动.
Spotify或overcast等其他应用不允许此行为.
到目前为止,我已经能够使用该异常更改控制中心的所有参数.有没有办法使这个滑块有用?
我正在使用类似下面的代码:
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; NSArray *commands = @[commandCenter.playCommand, commandCenter.pauseCommand, commandCenter.nextTrackCommand, commandCenter.previousTrackCommand, commandCenter.bookmarkCommand, commandCenter.changePlaybackPositionCommand, commandCenter.changePlaybackRateCommand, commandCenter.dislikeCommand, commandCenter.enableLanguageOptionCommand, commandCenter.likeCommand, commandCenter.ratingCommand, commandCenter.seekBackwardCommand, commandCenter.seekForwardCommand, commandCenter.skipBackwardCommand, commandCenter.skipForwardCommand, commandCenter.stopCommand, commandCenter.togglePlayPauseCommand]; for (MPRemoteCommand *command in commands) { [command removeTarget:nil]; [command setEnabled:NO]; } [commandCenter.playCommand addTarget:self action:@selector(playTrack)]; [commandCenter.pauseCommand addTarget:self action:@selector(pauseTrack)]; [commandCenter.playCommand setEnabled:YES]; [commandCenter.pauseCommand setEnabled:YES];
PatrickV.. 13
还有就是changePlaybackPositionCommand与关联事件MPChangePlaybackPositionCommandEvent.positionTime(见API https://developer.apple.com/library/ios/releasenotes/General/iOS91APIDiffs/Objective-C/MediaPlayer.html)
我试过了
[commandCenter.changePlaybackPositionCommand addTarget: self action: @selector( onChangePlaybackPositionCommand: )]
与相关的方法
- (MPRemoteCommandHandlerStatus) onChangePlaybackPositionCommand: (MPChangePlaybackPositionCommandEvent *) event { NSLog(@"changePlaybackPosition to %f", event.positionTime); return MPRemoteCommandHandlerStatusSuccess; }
但光标仍然不可移动,并且不调用该方法.我想我还是会想念一些东西
还有就是changePlaybackPositionCommand与关联事件MPChangePlaybackPositionCommandEvent.positionTime(见API https://developer.apple.com/library/ios/releasenotes/General/iOS91APIDiffs/Objective-C/MediaPlayer.html)
我试过了
[commandCenter.changePlaybackPositionCommand addTarget: self action: @selector( onChangePlaybackPositionCommand: )]
与相关的方法
- (MPRemoteCommandHandlerStatus) onChangePlaybackPositionCommand: (MPChangePlaybackPositionCommandEvent *) event { NSLog(@"changePlaybackPosition to %f", event.positionTime); return MPRemoteCommandHandlerStatusSuccess; }
但光标仍然不可移动,并且不调用该方法.我想我还是会想念一些东西
iOS 12和Swift 4+
以下是在远程播放中心中处理清理的一种改进方法:
// Handle remote events func setupRemoteTransportControls() { let commandCenter = MPRemoteCommandCenter.shared() // Scrubber commandCenter.changePlaybackPositionCommand.addTarget { [weak self](remoteEvent) -> MPRemoteCommandHandlerStatus in guard let self = self else {return .commandFailed} if let player = self.player { let playerRate = player.rate if let event = remoteEvent as? MPChangePlaybackPositionCommandEvent { player.seek(to: CMTime(seconds: event.positionTime, preferredTimescale: CMTimeScale(1000)), completionHandler: { [weak self](success) in guard let self = self else {return} if success { self.player?.rate = playerRate } }) return .success } } return .commandFailed } // Register to receive events UIApplication.shared.beginReceivingRemoteControlEvents() }