知道AVPlayer对象何时可以播放 [英] Knowing when AVPlayer object is ready to play

查看:216
本文介绍了知道AVPlayer对象何时可以播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试播放 MP3 文件,该文件从之前的<$ c传递到 UIView $ c> UIView (存储在 NSURL * fileURL 变量中)。

I'm trying to play an MP3 file that is passed to an UIView from a previous UIView (stored in a NSURL *fileURL variable).

I用初始化 AVPlayer

player = [AVPlayer playerWithURL:fileURL];

NSLog(@"Player created:%d",player.status);

NSLog 打印玩家创建:0,我认为这意味着它尚未准备好播放。

The NSLog prints Player created:0, which i figured means it is not ready to play yet.

当我点击播放 UIButton ,我运行的代码是:

When i click the play UIButton, the code i run is:

-(IBAction)playButtonClicked
{
    NSLog(@"Clicked Play. MP3:%@",[fileURL absoluteString]);

    if(([player status] == AVPlayerStatusReadyToPlay) && !isPlaying)
//  if(!isPlaying)
    {
        [player play];
        NSLog(@"Playing:%@ with %d",[fileURL absoluteString], player.status);
        isPlaying = YES;
    }
    else if(isPlaying)
    {

        [player pause];
        NSLog(@"Pausing:%@",[fileURL absoluteString]);
        isPlaying = NO;
    }
    else {
        NSLog(@"Error in player??");
    }

}

当我运行时,我总是在控制台中获取播放器中的错​​误??
如果我更换 if 条件,检查 AVPlayer 是否已准备好播放,只需 if(!isPlaying) ...,然后音乐播放第二次我点击播放 UIButton

When i run this, I always get Error in player?? in the console. If i however replace the if condition that checks if AVPlayer is ready to play, with a simple if(!isPlaying)..., then the music plays the SECOND TIME I click on the play UIButton.

控制台日志是:

Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 0**

Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Pausing:http://www.nimh.nih.gov/audio/neurogenesis.mp3

Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
2011-03-23 11:06:43.674 Podcasts[2050:207] Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 1**

我看到第二次, player.status 似乎持有1,我猜是 AVPlayerReadyToPlay

I see that the SECOND TIME, the player.status seems to hold 1, which I'm guessing is AVPlayerReadyToPlay.

如果我第一次点击播放 UIButton ,我该怎么做才能使播放正常工作?
(即,我如何确保 AVPlayer 不仅仅是创建,还准备好玩?)

What can I do to have the playing to work properly the first time i click the play UIButton? (ie, how can i make sure the AVPlayer is not just created, but also ready to play?)

推荐答案

您正在播放远程文件。 AVPlayer 可能需要一些时间来缓冲足够的数据并准备好播放该文件(参见 AV基金会编程指南

You are playing a remote file. It may take some time for the AVPlayer to buffer enough data and be ready to play the file (see AV Foundation Programming Guide)

但是你在点击播放按钮之前,似乎还没等待播放器准备就绪。我想要的是禁用此按钮并仅在播放器准备就绪时启用它。

But you don't seem to wait for the player to be ready before tapping the play button. What I would to is disable this button and enable it only when the player is ready.

使用KVO,可以通知播放器状态的变化:

Using KVO, it's possible to be notified for changes of the player status:

playButton.enabled = NO;
player = [AVPlayer playerWithURL:fileURL];
[player addObserver:self forKeyPath:@"status" options:0 context:nil];   

当状态改变时,将调用此方法:

This method will be called when the status changes:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    if (object == player && [keyPath isEqualToString:@"status"]) {
        if (player.status == AVPlayerStatusReadyToPlay) {
            playButton.enabled = YES;
        } else if (player.status == AVPlayerStatusFailed) {
            // something went wrong. player.error should contain some information
        }
    }
}

这篇关于知道AVPlayer对象何时可以播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆