簡體   English   中英

iOS 7使用AVPlayer在鎖定屏幕上回放歌曲

[英]iOS 7 rewind songs on lock screen using AVPlayer

我沒有找到如何從鎖定屏幕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;

每當播放速率改變(播放/暫停)時,執行

NSMutableDictionary *nowPlayingInfo = [[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo mutableCopy];
[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;
}

您必須在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
}

我似乎無法找到一種方法來添加拖動進度條以擦除當前正在播放的歌曲的功能,但是獲得顯示的進度條部分由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){};

BeginSeekingForward和BeginSeekingBackward分別由長按前進和后退按鈕觸發,當然EndSeekingForward和EndSeekingBackward事件子類型是手指從按鈕上抬起的時候。

獲得這些事件子類型后,將新的NSDictionary傳遞給

[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo

包含已經存在的所有信息(否則將傳遞您未傳入的屬性)以及新的MPNowPlayingInfoPropertyElapsedPlaybackTimeMPNowPlayingInfoPropertyPlaybackRate 對於速率,您可以決定滾動音頻的速度。 1是正常速度,負值向后播放,因此-2是反向的2倍正常速度。 您必須將MPNowPlayingInfoCenter的播放速率設置為與AVPlayer的播放速率相同,否則它們將不會排列。 這就是你想要傳遞當前時間的原因。 您可以通過以下方式獲得:

[player currentTime]

所以要設置當前時間和速率:

[nowPlayingInfo setObject:[player currentTime]
                   forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[nowPlayingInfo setObject:[[NSNumber alloc] initWithFloat:10]
                   forKey:MPNowPlayingInfoPropertyPlaybackRate];

並設置玩家的費率:

[player setRate:10]

這應該與播放器和進度條對齊,同時以10x向前滾動速度滾動。 因此,當您獲得BeginSeekingForward事件子類型時,您將執行此操作。 滾動結束時,將速率設置回一,但仍然將時間和其他信息傳遞給信息中心。 對於BeginSeekingBackward,只需使用負數作為費率。

我知道這是一個老帖子,但是沒有一個好的解決方案,我在搜索如何將音樂信息輸入到鎖定屏幕時遇到了這個線程。 我試圖在C#中使用Xamarin iOS實現此功能,並在http://www.sagorin.org/ios-playing-audio-in-background-audio上找到了一個很好的指南和示例Objective-C應用程序,其中包含一些此功能/ 但它沒有滾動功能,也沒有使用信息中心,只是從鎖定屏幕處理暫停/播放。 我在C#中使用Xamarin制作了一個示例應用程序,演示了如何向信息中心提供信息並使用鎖定和控制屏幕中的后退和前進按鈕進行滾動,您可以在此處獲取: https//github.com/jgold6/Xamarin- IOS的LockScreenAudio

我希望有人覺得這很有幫助。

以下是使用時間軸滑塊執行此操作的方法:

首先, nowPlayingInfo必須設置正確。 你可以在任何地方找到如何做到這一點(不要忘記密鑰MPNowPlayingInfoPropertyPlaybackRate );

第二是定義'滑動'的動作:

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

updatePlaybackRateMetadata方法必須更新MPNowPlayingInfoPropertyElapsedPlaybackTimeMPNowPlayingInfoPropertyPlaybackRate

很多時間過去了,但我希望這會有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM