6

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.

2
  • Do you want clarification on how to add the activity indicator? Are you getting callbacks on pressing play button and before starting player?
    – Amit
    Apr 25, 2013 at 5:30
  • Have you tried to use AVPlayerViewController? It already contains activity indicator that indicates all loading progress + AVPlayer. Sep 6, 2016 at 15:06

1 Answer 1

9

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
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.