问题描述
在MonoTouch中,我们遇到了电影播放器样本的问题,因为它只会播放一次视频,但不会再播放一次。
In MonoTouch, we ran into this problem with the Movie Player sample in that it would only play the video once, but would not play it a second time.
我问这个问题是为了发布答案,因为它已经打到了各种各样的人。
I am asking this question to post an answer, since it has been hitting various folks.
推荐答案
MPMoviePlayerController是引擎盖下的单身人士。如果您没有正确释放(ObjC)或Dispose()'d(MonoTouch)并且您创建了第二个实例,它将无法播放或仅播放音频。
MPMoviePlayerController is a singleton underneath the hood. If you have not properly release'd (ObjC) or Dispose()'d (MonoTouch) and you create a second instance, it will either not play, or play audio only.
此外,如果您订阅MPMoviePlayerScalingModeDidChangeNotification或MPMoviePlayerPlaybackDidFinishNotification或MPMoviePlayerContentPreloadDidFinishNotification,请注意已发布的NSNotification也会引用MPMoviePlayerController,因此如果您保留它,您将有一个引用播放器。
Additionally if you subscribe to MPMoviePlayerScalingModeDidChangeNotification or MPMoviePlayerPlaybackDidFinishNotification or MPMoviePlayerContentPreloadDidFinishNotification, be warned that the posted NSNotification takes a reference to the MPMoviePlayerController as well, so if you keep it around, you will have a reference the player.
虽然Mono的垃圾收集器最终会启动,但这是一个需要确定性终止的情况(你希望引用现在,当GC决定执行集合)。
Although Mono's Garbage Collector will eventually kick-in, this is a case where deterministic termination is wanted (you want the reference gone now, not gone when the GC decides to perform a collection).
这就是你想要在控制器上调用Dispose()方法,以及在通知上调用Dispose()方法的原因。
This is why you want to call the Dispose () method on the controller, and the Dispose() method on the notification.
例如:
// Deterministic termination, do not wait for the GC
if (moviePlayer != null){
moviePlayer.Dispose ()
moviePlayer = null;
}
如果您正在收听通知,请在通知处理程序中调用Dispose ,例如发布它保存到MPMoviePlayerController的引用:
If you were listening to notifications, call Dispose in your notification handler at the end, to release the reference that it keeps to your MPMoviePlayerController for example:
var center = NSNotificationCenter.DefaultCenter;
center.AddObserver (
"MPMoviePlayerPlaybackDidFinishNotification"),
(notify) => { Console.WriteLine ("Done!"); notify.Dispose (); });
这篇关于为什么不能多次使用MPMoviePlayerController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!