Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 7使用AVPlayer在锁屏上回放歌曲_Ios_Objective C_Avfoundation_Avplayer_Ios7 - Fatal编程技术网

iOS 7使用AVPlayer在锁屏上回放歌曲

iOS 7使用AVPlayer在锁屏上回放歌曲,ios,objective-c,avfoundation,avplayer,ios7,Ios,Objective C,Avfoundation,Avplayer,Ios7,我还没有找到从锁屏iOS 7倒带歌曲的方法。音量滑块可用,所有按钮工作正常,但上部滑块不可用 AVPlayer是我正在使用的类 感谢您的帮助 您需要在“正在播放信息”中设置此选项 每当项目(轨迹)更改时,执行以下操作 NSMutableDictionary *nowPlayingInfo = [[NSMutableDictionary alloc] init]; [nowPlayingInfo setObject:track.artistName forKey:MPMediaItemProper

我还没有找到从锁屏iOS 7倒带歌曲的方法。音量滑块可用,所有按钮工作正常,但上部滑块不可用

AVPlayer是我正在使用的类


感谢您的帮助

您需要在“正在播放信息”中设置此选项

每当项目(轨迹)更改时,执行以下操作

NSMutableDictionary *nowPlayingInfo = [[NSMutableDictionary alloc] init];
[nowPlayingInfo setObject:track.artistName forKey:MPMediaItemPropertyArtist];
[nowPlayingInfo setObject:track.trackTitle forKey:MPMediaItemPropertyTitle];
...
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.player.rate] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.currentPlaybackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfo;
每当播放速率改变(播放/暂停)时,请执行以下操作

您可以按如下方式获取当前播放时间:

- (NSTimeInterval)currentPlaybackTime {
  CMTime time = self.player.currentTime;
  if (CMTIME_IS_VALID(time)) {
    return time.value / time.timescale;
  }
  return 0;
}

我似乎也找不到一种方法来添加拖动进度条来拖动当前播放的歌曲的功能,但是上面的Chris部分介绍了如何显示进度条,但是您必须输入歌曲的持续时间以及其他信息才能显示进度条。因此,除了Chris指出的之外,您还需要在NSMutableDictionary中添加以下内容:

[nowPlayingInfo setObject:[track valueForProperty: MPMediaItemPropertyPlaybackDuration]
                   forKey:MPMediaItemPropertyPlaybackDuration];
此外,您还可以长按前进和后退按钮,滚动音乐。在:

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent
您可以查找以下事件子类型:

if (receivedEvent.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward){};
if (receivedEvent.subtype == UIEventSubtypeRemoteControlBeginSeekingForward){};
if (receivedEvent.subtype == UIEventSubtypeRemoteControlEndSeekingBackward){};
if (receivedEvent.subtype == UIEventSubtypeRemoteControlEndSeekingForward){};
BeginSekingForward和BeginSekingBackward分别由长按前进和后退按钮触发,当然,EndSeekingForward和EndSeekingBackward事件子类型是指手指从按钮上抬起时

获取这些事件子类型后,将新的NSDictionary传递给

[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo
其中包含所有已存在的信息(否则将清除未传入的属性),再加上新的
mpnowPlayingFoPropertyLapsedPlaybackTime
mpnowPlayingFoPropertyPlaybackRate
。对于速率,您可以决定滚动音频的速度。1是正常速度,负值向后播放,因此-2是反向正常速度的2倍。您必须将
mpnowplayingfocenter
的播放速率设置为与
AVPlayer
的播放速率相同,否则它们不会对齐。这就是为什么您还希望在当前时间内通过。您可以通过以下方式获得:

[player currentTime]
因此,要设置当前时间和速率:

[nowPlayingInfo setObject:[player currentTime]
                   forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[nowPlayingInfo setObject:[[NSNumber alloc] initWithFloat:10]
                   forKey:MPNowPlayingInfoPropertyPlaybackRate];
并将玩家的速率设置为:

[player setRate:10]
这将使播放器和进度条对齐,同时以10倍的向前滚动速度滚动。因此,当您获得BeginSekingForward事件子类型时,您将执行此操作。滚动结束后,将速率设置回1,但仍将时间和其他信息传递给信息中心。对于BeginSekingBackward,使用负数表示速率

我知道这是一篇老文章,但是没有一个好的解决方案,我在搜索如何将音乐信息放到锁屏上时遇到了这个问题。我试图用Xamarin iOS在C#中实现此功能,并在上找到了一个很好的指南和示例Objective-C应用程序,其中包含一些此功能。但它没有滚动功能,也没有利用信息中心,它只是覆盖了从锁屏处理暂停/播放。我用C#和Xamarin制作了一个示例应用程序,演示如何向信息中心提供信息,并使用锁和控制屏幕上的后退和前进按钮进行滚动,您可以在这里看到:


我希望有人觉得这有帮助

以下是如何使用时间轴滑块执行此操作:

首先,必须正确设置
nowPlayingInfo
。您可以在任何地方找到如何执行此操作(不要忘记键
mpnowplayingfopropertyplaybackrate

第二是定义“滑动”的动作:

MPChangePlaybackPositionCommand *changePlaybackPositionCommand = [[MPRemoteCommandCenter sharedCommandCenter] changePlaybackPositionCommand];
[changePlaybackPositionCommand addTarget:self action:@selector(changePlaybackPositionEvent:)];
[[MPRemoteCommandCenter sharedCommandCenter].changePlaybackPositionCommand setEnabled:YES];
在动作中,你可以做一些额外的事情,但这里最主要的是

[yourPlayer seekTo:event.positionTime completionHandler:^(BOOL finished) {
    [self updatePlaybackRateMetadata];
}];
updatePlaybackRate元数据
方法必须更新
mpnowPlayingFoPropertyLappedBlaybackTime
mpnowPlayingFoPropertyPlaybackRate


很多时间过去了,但我希望这会帮助一些人

您必须在swift中注册以下不同的事件

    MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
    MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
    MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
    MPRemoteCommandCenter.shared().stopCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
    MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
    MPRemoteCommandCenter.shared().changePlaybackPositionCommand.isEnabled = true
    MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget(self, action: #selector(self.handleChangePlaybackPositionRemoteCommandActions))

    let rcc = MPRemoteCommandCenter.shared()
    let skipBackwardIntervalCommand: MPSkipIntervalCommand? = rcc.skipBackwardCommand
    skipBackwardIntervalCommand?.isEnabled = false
    let skipForwardIntervalCommand: MPSkipIntervalCommand? = rcc.skipForwardCommand
    skipForwardIntervalCommand?.isEnabled = false
    let seekForwardCommand: MPRemoteCommand? = rcc.seekForwardCommand
    seekForwardCommand?.isEnabled = true
    rcc.changePlaybackPositionCommand.isEnabled = true
    rcc.changePlaybackRateCommand.isEnabled = true
    rcc.ratingCommand.isEnabled = true
    rcc.playCommand.isEnabled = true
    rcc.togglePlayPauseCommand.isEnabled = true
此处handleChangePlaybackPositionRemoteCommandActions此方法将是您的方法,当洗涤器(上滑块)更改其值时,必须管理歌曲搜索

它将如下所示:-

@objc func handleChangePlaybackPositionRemoteCommandActions(event:MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus
{
    print("handleChangePlaybackPositionRemoteCommandActions : \(event.positionTime)")
    self.appDelegate.audioPlayer?.seek(to: CMTime(seconds: event.positionTime, preferredTimescale: (self.appDelegate.audioPlayer?.currentItem?.currentTime().timescale)!))

    MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = event.positionTime

    return MPRemoteCommandHandlerStatus.success
}

这适用于您使用MediaPlayer时,他们使用的是AVPlayer。
MPNowplayingInfo
可用于任何播放器。用
AVPlayer
这样做。我用同样的方法做,但是进度滑块仍然不可用。。。我做错了吗?抱歉,这并没有回答如何使滑块条在锁定屏幕上时可拖动以控制轨迹内的位置的问题。默认音乐播放器具有垂直刻度线更大且可以拖动的功能。这是一个笨拙的界面,与幻灯片解锁功能冲突,很难抓住微小的记号(进度拇指),但默认应用程序提供的功能似乎没有自定义开发音乐应用程序的界面-RRH部分有效。时间与玩家完全同步。但拖拉仍然是一个问题。
@objc func handleChangePlaybackPositionRemoteCommandActions(event:MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus
{
    print("handleChangePlaybackPositionRemoteCommandActions : \(event.positionTime)")
    self.appDelegate.audioPlayer?.seek(to: CMTime(seconds: event.positionTime, preferredTimescale: (self.appDelegate.audioPlayer?.currentItem?.currentTime().timescale)!))

    MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = event.positionTime

    return MPRemoteCommandHandlerStatus.success
}