87

I am developing an iPhone application that needs to play videos. So far, I learned that there are at least two API's for achieving this; AVPlayer and MPMoviePlayerController.

What are the main differences?

Till
  • 27,559
  • 13
  • 88
  • 122
suse
  • 10,503
  • 23
  • 79
  • 113
  • 4
    Tried to reword this question to make sure it is not generating opinion based answers. – Till Dec 12 '13 at 18:07

1 Answers1

173

NOTE as of iOS9, Apple has deprecated the MPMoviePlayerController:

The MPMoviePlayerController class is formally deprecated in iOS 9. (The MPMoviePlayerViewController class is also formally deprecated.) To play video content in iOS 9 and later, instead use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit.

Copied from the MPMoviePlayerController reference.

AVPlayer

AVPlayer gives you a lot more flexibility but is pretty poorly documented. Using this API will force you to create your own UI. AVFoundation (the framework that brings you AVPlayer) generally is a bit hard on the user (coder) as it forces you to use Key-Value Observing a lot for checking states. The concept of KVO is great, do not get me wrong - still, for unexperienced developers it can be painful to learn. Apple sometimes omits the information on which properties are actually KVO compliant and that will force you to do some experimentation.

One big advantage of AVPlayer over MPMoviePlayerController would for example be its extended version, AVQueuePlayer as that one is able to do gapless playback of multiple movie sources. Another advantage certainly is the feature rich AVFoundation framework allowing you to do things like on-the-fly movie composition / encoding / converting.

Yet another huge advantage of AVPlayer is the fact that you may actually play multiple video sources concurrently (e.g. side by side) without any problem.

MPMoviePlayerController

MPMoviePlayerController is easy to use and covers most needs out of the box. Using this API will give you a good looking and commonly understood UI. The UI however can be disabled and or replaced with a custom one.

For status changes, MPMoviePlayerController uses a few NSNotifications covering everything the regular App needs.

Under the hood, MPMoviePlayerController builds on top of AVPlayer - but that actually happens entirely transparent to the user - you have no access to that layer while using MPMoviePlayerController.

MPMoviePlayerController uses the underlaying AVPlayer as a singleton instance, hence it is not possible to use multiple instances of MPMoviePlayerController to play videos concurrently.

On the other hand, as soon as you are trying to extend the functionality of MPMoviePlayerController with your own features, code quickly gets nasty - e.g. you will possibly start using multiple timers for covering things like a proper starve-detection (actually, that feature got included into iOS5's version of this class), custom UI updates, ... Or you may end up having more than a handful of state properties trying to cover things like gracefully aborting of the playback while the player is still pre-buffering.


Personal Recommendation

I have used both and I will continue to use both, depending on the needs of the App I have the pleasure to build. For most (simple) projects, I would recommend using MPMoviePlayerController over AVPlayer as it is very simple to use and with just a few lines of code, you get a full-fledged media player. And if your demands on media playback are even simpler, have a peek at MPMoviePlayerViewController (note that View-part).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Till
  • 27,559
  • 13
  • 88
  • 122
  • 2
    AVPlayer also automatically lets you use layer animations. If you want to run Core Animation in the background, don't use an MPMoviePlayerController, though it might have a setting to enable them, just be aware of that if you're using CALayer stuff – Stephen J Apr 12 '12 at 20:48
  • Shouldn't it be: The UI however **can't** be disabled and or replaced with a custom one? – Besi Mar 06 '13 at 06:44
  • 3
    No. The answer is correct. The MPMoviePlayerController UI **can** be disabled and or replaced with a custom one. – Dave Batton Mar 14 '13 at 21:58
  • 1
    @Besi no, entirely correct as written - it definitely can be disabled (using MPMovieControlStyleNone) and hence replaced as there is nothing stopping you from adding your own view as a sibling of MPMoviePlayerController's view or directly on its `backgroundView`. – Till Mar 14 '13 at 22:25
  • I am having a hard time capturing a screen shot of a MPMoviePlayerController, I dont need a thumbnail what I need is a screenshot of the video itself. I wonder if AVPlayer will allow me to do what I need. Thanks. – SleepNot Apr 16 '13 at 06:44
  • 8
    As of iOS 9 MPMoviePlayerController is deprecated and so you should use AVPlayer for future projects – lewis Sep 18 '15 at 20:24
  • 1
    That is super valuable information @lewis42 - will add it to my ancient posting :) – Till Sep 21 '15 at 19:57