我开发了一款 radio 应用程序,它正在运行。
我想放置一个事件指示器 View ,以便当您触摸播放时它会启动事件指示器 View ,而当音频开始时,事件指示器会停止。
最佳答案
这会将事件 View 添加到 View 的中心。在您处理播放按钮的“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
}
}
}
关于ios - 在 AVplayer 准备好播放音乐时添加事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16206893/