为什么不可能多次使用MPMoviePlayerController?

在MonoTouch中,我们遇到了这个问题与电影播放器​​样品,它只会播放一次的video,但不会播放第二次。

我正在问这个问题发表一个答案,因为它已经打了各种各样的人。

MPMoviePlayerController是一个单身人士。 如果您没有正确释放(ObjC)或Dispose()'d(MonoTouch),并且您创build了第二个实例,它将不会播放或仅播放audio。

此外,如果您订阅了MPMoviePlayerScalingModeDidChangeNotification或MPMoviePlayerPlaybackDidFinishNotification或MPMoviePlayerPlaybackDidFinishNotification或MPMoviePlayerContentPreloadDidFinishNotification,请注意发布的NSNotification也需要对MPMoviePlayerController的引用,所以如果保留它,您将有一个对玩家的引用。

尽pipeMono的垃圾收集器最终会启动,但是这是一个需要确定性终止的情况(您希望当前的参考消失,而GC决定执行一个收集时不会消失)。

这就是为什么要调用控制器上的Dispose()方法和通知上的Dispose()方法。

例如:

// Deterministic termination, do not wait for the GC if (moviePlayer != null){ moviePlayer.Dispose () moviePlayer = null; } 

如果您正在侦听通知,请在通知处理程序中调用Dispose,以释放它保留给MPMoviePlayerController的引用,例如:

 var center = NSNotificationCenter.DefaultCenter; center.AddObserver ( "MPMoviePlayerPlaybackDidFinishNotification"), (notify) => { Console.WriteLine ("Done!"); notify.Dispose (); }); 

无法看到您的代码尼尔,我没有编辑权限,所以这里又是:

秘密在endPlay中设置:moviePlayer.initialPlaybackTime = -1; 在发布之前。 试试看 : :)

 -(void)playMovie:(NSString *)urlString{ movieURL = [NSURL URLWithString:urlString]; moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; moviePlayer.initialPlaybackTime = 0; //Register to receive a notification when the movie has finished playing. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endPlay:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; moviePlayer.scalingMode = MPMovieScalingModeAspectFit; moviePlayer.movieControlMode = MPMovieControlModeDefault; moviePlayer.backgroundColor = [UIColor blackColor]; [moviePlayer play]; } -(void)endPlay: (NSNotification*)notification{ [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; moviePlayer.initialPlaybackTime = -1; [moviePlayer stop]; [moviePlayer release]; } 

秘密在endPlay中设置:moviePlayer.initialPlaybackTime = -1; 在发布之前。 试试看 : :)

 -(void)playMovie:(NSString *)urlString{ movieURL = [NSURL URLWithString:urlString]; moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; moviePlayer.initialPlaybackTime = 0; //Register to receive a notification when the movie has finished playing. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endPlay:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; moviePlayer.scalingMode = MPMovieScalingModeAspectFit; moviePlayer.movieControlMode = MPMovieControlModeDefault; moviePlayer.backgroundColor = [UIColor blackColor]; [moviePlayer play]; } -(void)endPlay: (NSNotification*)notification{ [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; moviePlayer.initialPlaybackTime = -1; [moviePlayer stop]; [moviePlayer release]; }