在 moviePlayerController 中隐藏 ActivityIndicator
-
13-09-2020 - |
题
如您所知,当我使用 MPmoviePlayerController 播放电影时,moviePlayer 应该在 moviePlayer 视图的中心显示一个 ActivityIndicatorView。现在,我在我的程序中放置了一个自定义的activityIndicatorView,我只想隐藏或删除MPMoviePlayController的activityIndicatorView,我可以这样做吗?
解决方案
我们可以!
我想您想要做的是在加载电影时显示活动指示器,而不是在播放时显示活动指示器?我只是假设并继续......
在 SDK 3.2 及更高版本中,整个 MPMoviePlayerController (和 MPMoviePlayerViewController)比以前的版本好很多。如果您仍在使用 MPMoviePlayerController,您可能会考虑切换到 MPMoviePlayerViewController(它基本上是封装 MPMoviePlayerController 对象的 UIView 子类)。
无论如何,要显示和隐藏 UIActivityindicator 视图,我建议您连接到加载或播放状态更改时从 MPMoviePlayerController 发送的通知。
其中一些是:
MPMoviePlayerPlaybackStateDidChangeNotification
MPMoviePlayerLoadStateDidChangeNotification
所以你可以连接到这些事件来执行以下操作:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(loadStateChanged:)
name: MPMoviePlayerLoadStateDidChangeNotification
object: moviePlayerViewController.moviePlayer];
和这个
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(playBackStateChanged:)
name: MPMoviePlayerPlaybackStateDidChangeNotification
object: moviePlayerViewController.moviePlayer];
并在你的处理程序中(playBackStateChanged
和 loadStateChanged
)
你可以这样做:
-(void)playBackStateChanged:(id)sender
{
MPMoviePlaybackState playbackState = [moviePlayerViewController.moviePlayer playbackState];
switch (playbackState) {
case MPMoviePlaybackStateStopped :
break;
case MPMoviePlaybackStatePlaying :
[yourActivityIndicatorView stopAnimating];
break;
case MPMoviePlaybackStateInterrupted :
[yourActivityIndicatorView startAnimating];
break;
}
}
确保 IndicatorView 的“hidesWhenStopped”(或类似)属性设置为 yes(如果这样做,则不必关心隐藏和取消隐藏控件。
其余的很简单,只需将 ActivityIndicatorView 添加到 MPMovieViewController 的视图之上即可。
希望我能帮忙
干杯
萨姆