首页 文章

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

提问于
浏览
1

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

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

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

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

status:表示播放器是否可用于播放 .

AVPlayerStatusUnknown,
AVPlayerStatusReadyToPlay,
AVPlayerStatusFailed

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

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

非常感谢!

2 回答

  • 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

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

相关问题