当前位置:  开发笔记 > 前端 > 正文

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

如何解决《如何使控制中心滑块可编辑?》经验,为你挑选了2个好方法。

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

在此输入图像描述

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

但光标仍然不可移动,并且不调用该方法.我想我还是会想念一些东西



1> PatrickV..:

还有就是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;
}

但光标仍然不可移动,并且不调用该方法.我想我还是会想念一些东西



2> Hannes Sverr..:

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

推荐阅读
帆侮听我悄悄说星星
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有