为什么不能多次使用 MPMoviePlayerController? [英] Why is it not possible to use the MPMoviePlayerController more than once?

查看:18
本文介绍了为什么不能多次使用 MPMoviePlayerController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MonoTouch 中,我们在 Movie Player 示例中遇到了这个问题,因为它只会播放视频一次,但不会播放第二次.

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() (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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆