Adding activity indicator while AVplayer gets ready to play music

audio-streaming, avplayer, ios

Solution

This will add the activity view to the center of the view. Add this code in the "IBAction" where u handle the play button.

UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    activityView.center=self.view.center;

    [activityView startAnimating];

    [self.view addSubview:activityView];
    [self.view userInteractionEnabled:NO];   //to avoid touch events when activity is on

And to stop the activity indicator use

[activityView stopAnimating];    
[activityView removeFromSuperview];
[self.view userInteractionEnabled:YES];

This is from this link about AV Foundation. and this answer ->

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

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

In the PLAY button's event add the code to start the `UIActivityIndicator`

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) {
            //DISABLE THE UIACTIVITY INDICATOR HERE
        } else if (player.status == AVPlayerStatusFailed) {
            // something went wrong. player.error should contain some information
        }
    }
}

Problem

I developed an app for radio and it's working. I want to put an activity indicator view so that when you touch the play it starts the activity indicator view and when the audio starts, the activity indicator stops.

Original source

Related problems