如何从AVPlayer获取音频流状态的通知?

时间:2012-03-20 11:38:49

标签: iphone audio streaming avplayer

我正在使用AVPlayer来传输一些实时HTTP音频,而不是不支持实时HTTP音频流的AVAudioPlayer,问题是,如何获取当前播放的状态?例如:

点按播放按钮 - > [加载] - > [播放] 点按暂停按钮 - > [暂停]

我需要在加载时显示微调器,在播放时显示暂停按钮并在暂停时显示播放按钮,我知道我可以观察AVPlayer的“状态”和“速率”属性:

率: 目前的播放速度。 0.0表示“已停止”,1.0表示“以当前项目的自然速率进行游戏”。

状态: 指示播放器是否可用于播放。

AVPlayerStatusUnknown,
AVPlayerStatusReadyToPlay,
AVPlayerStatusFailed

因此无法指示音频是“正在加载”,并且在状态更改为AVPlayerStatusReadyToPlay后,仍需要一些时间才能播放音频(可能是因为它是实时音频)。

但无论如何,我如何获得当前播放的正确状态?我知道Matt有一个AudioStream,但它不支持HTTP Live音频。

非常感谢!

2 个答案:

答案 0 :(得分:3)

我用过

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

这对我有用......我希望对你有所帮助。

答案 1 :(得分:1)

针对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)
    }

}