0
我有一個問題,在UIScrollView中,我有一些視圖(視頻控制器在下面)充當視頻內容的頁面(最初顯示縮略圖視圖)。MPMoviePlayerController有時播放音頻,而不是在淡入淡出時的視頻
有時(但並非總是)當我滾動到下一頁時,視頻的音頻開始播放,但沒有視覺效果。這似乎並不當我不褪色的視圖中的發生
視頻初始化代碼爲:
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] init];
self.moviePlayer = moviePlayer;
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[moviePlayer.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
moviePlayer.backgroundView.backgroundColor = [UIColor clearColor];
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
moviePlayer.contentURL = [NSURL URLWithString:[videoData.video src]];
[self.videoController.view addSubview:self.moviePlayer.view];
[self.videoController.view bringSubviewToFront:moviePlayer.view];
[moviePlayer.view setAlpha:0.0f];
// Movie Observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkMovieStatus:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
// Add tap handler
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onPlayerTapped:)];
singleFingerTap.numberOfTapsRequired = 1;
singleFingerTap.delegate = self;
[self.moviePlayer.view addGestureRecognizer:singleFingerTap];
[moviePlayer prepareToPlay];
和播放代碼(儘量避免黑色閃光在視頻的開始)是:
- (void)checkMovieStatus:(NSNotification *)notification {
if(self.moviePlayer.readyForDisplay && (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK)) {
[self.moviePlayer play];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[UIView animateWithDuration:0.4f animations:^{
[self.moviePlayer.view setAlpha:1.0f];
} completion:^(BOOL finished) {
//[self.moviePlayer play]; // tried here as well
}];
}
}
代碼,以消除滾動視頻的看法是:
- (void)removeMoviePlayer {
if(self.moviePlayer) {
[self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview];
self.moviePlayer = nil;
}
}