【问题标题】:How to change track position on lock screen/control center?如何更改锁定屏幕/控制中心的轨道位置?
【发布时间】:2013-10-25 13:10:02
【问题描述】:

使用 ios 7 音乐应用播放歌曲时,用户可以使用滑块在锁定屏幕/控制中心更改歌曲位置。滑块处于活动状态:

但是在我的应用中播放音乐时,用户无法做到这一点。滑块未激活:

如何在我的应用中启用这些功能?

【问题讨论】:

  • 您在应用中播放音乐的效果如何?
  • @WDUK 通过 MPMoviePlayerController
  • @NeimanAleksei 你如何显示歌曲名称和歌曲时长?

标签: ios ios7 remote-control control-center


【解决方案1】:

您可以在 iOS 9.1 及更高版本上借助 MPRemoteCommandCenter 更改轨道位置。

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_0) {
            MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
            [commandCenter.changePlaybackPositionCommand setEnabled:true];
            [commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)];
        }

和方法

- (MPRemoteCommandHandlerStatus)changedThumbSliderOnLockScreen:(MPChangePlaybackPositionCommandEvent *)event
{
    // change position
    [self setCurrentPlaybackTime:event.positionTime];
    // update MPNowPlayingInfoPropertyElapsedPlaybackTime
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

    return MPRemoteCommandHandlerStatusSuccess;
}

【讨论】:

    【解决方案2】:

    swift4 您可以在 iOS 9.1 及更高版本上借助 MPRemoteCommandCenter 更改轨道位置。

    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.changePlaybackPositionCommand.isEnabled = true
    commandCenter.changePlaybackPositionCommand.addTarget(
     self, action:#selector(changePlaybackPositionCommand(_:)))
    

    和方法

    @objc func changePlaybackPositionCommand(_ event:
              MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus {
        let time = event.positionTime
        //use time to update your track time
        return MPRemoteCommandHandlerStatus.success
    }
    

    请注意,如果您希望启用该推荐,则必须对 commandCenter 中的每个命令执行此操作。

    【讨论】:

      【解决方案3】:

      我一直在寻找同样的东西,但我认为这是不可能的,请参阅这篇文章:

      How to enable audio scrubber in iOS Lock Screen control panel?

      Spotify 和 Soundcloud 等流行应用也没有实现此功能。

      如果您正在寻找一种在锁定屏幕上显示当前音乐的方法,您需要执行以下操作。

      播放新曲目时首先更新 NowPlayingInfo :

      NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
      
          [songInfo setObject:trackTitle forKey:MPMediaItemPropertyTitle];
          [songInfo setObject:artistName forKey:MPMediaItemPropertyArtist];
          [songInfo setObject:duration forKey:MPMediaItemPropertyPlaybackDuration];
          [songInfo setObject:releaseDate forKey:MPMediaItemPropertyReleaseDate];
          [songInfo setValue:playbackRate forKey:MPNowPlayingInfoPropertyPlaybackRate];
          [songInfo setObject:elapsedTime forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
          [songInfo setObject:albumArtImage forKey:MPMediaItemPropertyArtwork];
          [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
      

      要处理来自锁屏的事件,您首先需要告诉您的应用开始接收来自遥控器的事件。我使用以下代码在我的 AppDelegate 的应用程序 didFinishLaunchingWithOptions 中执行此操作

       // Turn on remote control event delivery
      [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
      

      接下来你需要实现remoteControlReceivedWithEvent 方法来处理捕获的事件。在 APPDelegate 中添加如下方法

      - (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
      
       if (receivedEvent.type == UIEventTypeRemoteControl) {
      
          switch (receivedEvent.subtype) {
              case UIEventSubtypeRemoteControlPause:
                  //pause code here
                  break;
      
              case UIEventSubtypeRemoteControlPlay:
                   //play code here
                  break;
      
              case UIEventSubtypeRemoteControlPreviousTrack:
                  // previous track code here
                  break;
      
              case UIEventSubtypeRemoteControlNextTrack:
                  //next track code here
                  break;
      
              default:
                  break;
          }
       }
      

      }

      苹果文档中有关 MPNowPlayingInfoCenter 的更多信息 -> https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPNowPlayingInfoCenter_Class

      【讨论】:

      • 如何使用remoteControlReceivedWithEvent
      • @KrunalNagvadia in swift : 覆盖 func remoteControlReceived(with event: UIEvent?) { switch event?.subtype { case .remoteControlPlay: print("") default: break } }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      相关资源
      最近更新 更多