如何使控制中心滑块可编辑?

时间:2015-12-16 20:04:17

标签: ios objective-c

我正在开发一个能播放声音文件的应用。如果我打开苹果音乐应用程序,滑块让我在我所在的歌曲之间移动。

enter image description here

其他应用如spotify或overcast不允许这种行为。

enter image description here

到目前为止,我已经能够使用该异常更改控制中心的所有参数。有没有办法使这个滑块有用?

我正在使用类似下面的代码:

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

6 个答案:

答案 0 :(得分:10)

changePlaybackPositionCommand API及相关的事件MPChangePlaybackPositionCommandEvent.positionTime(请参阅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;
}

但光标仍然无法移动,并且未调用该方法。我想我还是想念一些东西

答案 1 :(得分:8)

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()
}

答案 2 :(得分:4)

除了实现回调之外,您似乎必须将canBeControlledByScrubbing属性设置为true。遗憾的是,没有公共访问者可以设置它,所以您必须按照以下方式执行此操作(并且无法将您的应用程序提交到AppStore):

NSNumber *shouldScrub = [NSNumber numberWithBool:YES];
[[[MPRemoteCommandCenter sharedCommandCenter] changePlaybackPositionCommand]
     performSelector:@selector(setCanBeControlledByScrubbing:) withObject:shouldScrub];
[[[MPRemoteCommandCenter sharedCommandCenter] changePlaybackPositionCommand]
            addTarget:self
               action:@selector(handleChangePlaybackPositionCommand:)];

如果您这样做,您将获得handleChangePlaybackPositionCommand:方法的回调,并以MPChangePlaybackPositionCommandEvent为唯一参数。

如果您确实希望支持早于iOS的iOS版本,我建议在执行上述代码之前检查iOS版本以防止崩溃(您可以使用iOS 8中引入的新API来执行此操作,或者如果你想支持iOS 7及更早版本,请使用类似

的内容
[[[UIDevice currentDevice] systemVersion] compare:@"9.1" options:NSNumericSearch] != NSOrderedAscending

我希望这会有所帮助: - )

答案 3 :(得分:1)

从2020年开始使用Swift 5,如果我们仅使用commandCenter.changePlaybackPositionCommand将无法正常工作,我们还需要为播放器设置MPMediaItemPropertyPlaybackDuration的元数据,那么我们就可以使用滑块。

从Apple查看此文章: https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/creating_a_basic_video_player_ios_and_tvos/controlling_background_audio

查看以下部分:Provide Display Metadata

答案 4 :(得分:0)

没有API可以支持 - 正如您所注意到的,内置的音乐应用程序是唯一可以使用它的人。如果您想要添加API,最好选择file an enhancement request

更新:看起来像iOS 9.1这可能不再是这种情况 - 请参阅PatrickV的回答。

答案 5 :(得分:0)

我已经在这里command center Scrubber on lock screen swift

回答了

基本上,您只需要nowPlaying元数据,然后为每个要启用它的功能添加一个处理程序即可。

当我看到Librivox应用程序具有正常工作的洗涤器时,我意识到对这仅适用于Apple的评论是错误的。