在AVplayer准备播放音乐时添加活动指示器

时间:2013-04-25 05:19:59

标签: ios audio-streaming avplayer

我开发了一个广播应用程序,它正在运行。

我想放置一个活动指示器视图,以便在您触摸播放时启动活动指示器视图,当音频开始时,活动指示器将停止。

1 个答案:

答案 0 :(得分:9)

这会将活动视图添加到视图的中心。在“IBAction”中添加此代码,您可以在其中处理播放按钮。

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

并停止使用活动指标

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

这是this link about AV Foundation。和this answer - >

使用KVO,可以通知玩家状态的变化:

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

PLAY按钮的事件中添加代码以启动UIActivityIndicator

状态更改时将调用此方法:

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