How to use AVPlayer correctly so that it doesn't show white screen before playing video?

Every time I try to play a megabyte video with AVPlayer

it first shows a white screen for a second and then starts the video.

Why does this happen if the video is already cached? Is there a way to stop this so that it goes straight to the video without showing a white screen?

I tried to use AVPlayer

isReady

to check the status AVPlayer

and play the video only when ready, but it still shows a white screen.

Also every time I try to get the duration of the videos to be played through AVPlayer

, I get 0.0 seconds all the time, so I can't add a timer to the video either because I can't get the duration of the video as it keeps displaying a white screen on give me a sec.

+3


source to share


4 answers


First, it AVPlayer

does not show a white screen, its white background. So basically yours is AVPlayer

delayed. I think you'll click UIButton

and then load the file in AVPlayer

and start playing right away. This is where the problem lies. It AVPlayer

may take some time to store enough data and be ready to play the file. Using KVO, you can receive notifications about changes in player status.

So, first you need to disable the play button, load AVPlayer

and add an observer:

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

      



Then enable it after checking AVPlayerStatusReadyToPlay

:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    if (object == player && [keyPath isEqualToString:@"status"]) {
        if (player.status == AVPlayerStatusReadyToPlay) {
            play.enabled = YES;
        }
    }
}

      

+2


source


I know this is an old question, but I get the same problem even with correct detection when the AVPlayer is ready to play.

I wanted it to render the image in such a way that there was a smooth transition between the original static image and the fade.

The trick for me was to set a crisp background with



AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[controller.view setBackgroundColor:[UIColor clearColor]];

      

This way, if I toggle the visibility of the player when it's ready to play, I never see a black or white screen because the player has a clear background, making it a smooth transition!

+1


source


Blancos is right. AVPlayer takes time to reach AVPlayerStatusReadyToPlay. So, initialize the player with url and only play it when it is AVPlayerStatusReadyToPlay.

player = [AVPlayer playerWithURL:URL];  
[player addObserver:self forKeyPath:@"status" options:0 context:nil];   

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context   
{  
    if (object == player && [keyPath isEqualToString:@"status"]) {  
        if (player.status == AVPlayerStatusReadyToPlay) {  
            player.play()  
        }  
    }  
}

      

0


source


I had the same problem. In order not to add KVO, I just install AVPlayer when the url is set like this ...

var urlToUse: NSURL?
{
    didSet
    {
        guard let urlToUse = urlToUse else { return }
        replayPlayer = AVPlayer(URL: urlToUse)
    }
}

      

Thus, the AVPlayer status will be ready when needed.

0


source







All Articles