【问题标题】:Adding activity indicator while AVplayer gets ready to play music在 AVplayer 准备播放音乐时添加活动指示器
【发布时间】:2013-04-18 21:12:42
【问题描述】:

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

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

【问题讨论】:

  • 您想了解如何添加活动指示器吗?您是否在按下播放按钮和开始播放器之前收到回调?
  • 你试过用AVPlayerViewController吗?它已经包含指示所有加载进度的活动指示器 + AVPlayer。

标签: ios audio-streaming avplayer


【解决方案1】:

这会将活动视图添加到视图的中心。将此代码添加到您处理 播放按钮的“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
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多