簡體   English   中英

如何從AVPlayer獲取音頻流狀態的通知?

[英]How to get notification for audio streaming status from AVPlayer?

我正在使用AVPlayer來傳輸一些實時HTTP音頻,而不是AVAudioPlayer,它不支持實時HTTP音頻流,問題是,我如何獲得當前播放的狀態? 例如:

點擊播放按鈕 - > [加載] - > [播放]點擊暫停按鈕 - > [已暫停]

我需要在加載時顯示微調器,在播放時顯示暫停按鈕並在暫停時顯示播放按鈕,我知道我可以觀察AVPlayer的“狀態”和“速率”屬性:

rate:當前的播放速度。 0.0表示“已停止”,1.0表示“以當前項目的自然速率進行游戲”。

status:表示播放器是否可用於播放。

AVPlayerStatusUnknown,
AVPlayerStatusReadyToPlay,
AVPlayerStatusFailed

因此無法指示音頻是“正在加載”,並且在狀態更改為AVPlayerStatusReadyToPlay后,仍然需要一些時間來播放音頻(可能因為它是實時音頻)。

但無論如何,我如何獲得當前播放的正確狀態? 我知道Matt有一個AudioStream,但它不支持HTTP Live音頻。

非常感謝!

我用了

    [self.mPlayerItem addObserver:self 
                   forKeyPath:kStatusKey 
                      options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                      context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];

監控狀態鍵(“狀態”)。 然后我創建了播放器

[self setPlayer:[AVPlayer playerWithPlayerItem:self.mPlayerItem]];  

並在observeValueForKeyPath中

    if (context == AVPlayerDemoPlaybackViewControllerStatusObservationContext)
{        
    AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
    switch (status)
    {
            /* Indicates that the status of the player is not yet known because 
             it has not tried to load new media resources for playback */
        case AVPlayerStatusUnknown:
        {
            [lblvalidation setText:@"Loading..."];

            NSLog(@"AVPlayerStatusUnknown");
        }
            break;

        case AVPlayerStatusReadyToPlay:
        {
            /* Once the AVPlayerItem becomes ready to play, i.e. 
             [playerItem status] == AVPlayerItemStatusReadyToPlay,
             its duration can be fetched from the item. */

            NSLog(@"AVPlayerStatusReadyToPlay");

            [self.player play];
            [lblvalidation setText:@"Playing..."];
        }
            break;

        case AVPlayerStatusFailed:
        {
            [lblvalidation setText:@"Error..."];
            NSLog(@"AVPlayerStatusFailed");
        }
            break;
    }
}

這適合我...我希望它對你有所幫助。

針對Swift 2進行了更新:

 private var AVPlayerDemoPlaybackViewControllerStatusObservationContext = 0

添加觀察者:

player.currentItem!.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: &AVPlayerDemoPlaybackViewControllerStatusObservationContext)

觀察

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

    if context == &AVPlayerDemoPlaybackViewControllerStatusObservationContext {
        if let change = change as? [String: Int]
        {
            let status = change[NSKeyValueChangeNewKey]!

            switch status {
            case AVPlayerStatus.Unknown.rawValue:
                print("The status of the player is not yet known because it has not tried to load new media resources for playback")

            case AVPlayerStatus.ReadyToPlay.rawValue:
                self.playButtonPressed(playButton)
                print("The player is Ready to Play")

            case AVPlayerStatus.Failed.rawValue:
                print("The player failed to load the video")

            default:
                print("Other status")
            }
        }
    } else {
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
    }

}

暫無
暫無

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

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