Why is it not possible to use the MPMoviePlayerController more than once?

前端 未结 3 1943
旧时难觅i
旧时难觅i 2020-12-31 10:07

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 thi

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 10:42

    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.

    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.

    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).

    This is why you want to call the Dispose () method on the controller, and the Dispose() method on the notification.

    For example:

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

    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 (); });
    

提交回复
热议问题