ios AVPlayer 和 MPMoviePlayerController 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8146942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:24:43  来源:igfitidea点击:

AVPlayer and MPMoviePlayerController differences

iosios4mpmovieplayercontrolleravfoundationavplayer

提问by suse

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; AVPlayerand MPMoviePlayerController.

我正在开发一个需要播放视频的 iPhone 应用程序。到目前为止,我了解到至少有两个 API 可以实现这一点;AVPlayerMPMoviePlayerController

What are the main differences?

主要区别是什么?

回答by Till

NOTEas of iOS9, Apple has deprecated the MPMoviePlayerController:

注意从 iOS9 开始,Apple 已弃用 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.

MPMoviePlayerController 类在 iOS 9 中正式弃用。(MPMoviePlayerViewController 类也正式弃用。)要在 iOS 9 及更高版本中播放视频内容,请改用来自 AVKit 框架的 AVPictureInPictureController 或 AVPlayerViewController 类,或来自 WebKit 的 WKWebView 类。

Copied from the MPMoviePlayerController reference.

复制自MPMoviePlayerController 参考

AVPlayer

影音播放器

AVPlayergives 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 Observinga 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.

AVPlayer为您提供了更多的灵活性,但记录很差。使用此 API 将迫使您创建自己的 UI。AVFoundation(为您带来的框架AVPlayer)通常对用户(编码人员)来说有点困难,因为它迫使您大量使用键值观察来检查状态。KVO 的概念很棒,不要误会我的意思——不过,对于没有经验的开发人员来说,学习起来可能会很痛苦。Apple 有时会忽略有关哪些属性实际上符合 KVO 的信息,这将迫使您进行一些实验。

One big advantage of AVPlayerover MPMoviePlayerControllerwould for example be its extended version, AVQueuePlayeras that one is able to do gapless playback of multiple movie sources. Another advantage certainly is the feature rich AVFoundationframework allowing you to do things like on-the-fly movie composition / encoding / converting.

例如,AVPlayerover 的一大优势MPMoviePlayerController是它的扩展版本,AVQueuePlayer因为它能够无缝播放多个电影源。另一个优势当然是功能丰富的AVFoundation框架,允许您执行诸如动态电影合成/编码/转换之类的操作。

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

另一个巨大的优势AVPlayer是您实际上可以同时(例如并排)播放多个视频源而没有任何问题。

MPMoviePlayerController

MP电影播放器​​控制器

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

MPMoviePlayerController易于使用,开箱即可满足大多数需求。使用此 API 将为您提供美观且易于理解的 UI。然而,用户界面可以被禁用和/或替换为自定义的。

For status changes, MPMoviePlayerControlleruses a few NSNotificationscovering everything the regular App needs.

对于状态更改,MPMoviePlayerController使用一些NSNotifications涵盖常规应用程序所需的所有内容。

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

在引擎盖下,MPMoviePlayerController构建在之上AVPlayer- 但实际上这对用户完全透明 - 在使用MPMoviePlayerController.

MPMoviePlayerControlleruses the underlaying AVPlayeras a singleton instance, hence it is not possible to use multiple instances of MPMoviePlayerControllerto play videos concurrently.

MPMoviePlayerController使用底层AVPlayer作为单例实例,因此不可能使用多个实例MPMoviePlayerController同时播放视频。

On the other hand, as soon as you are trying to extend the functionality of MPMoviePlayerControllerwith 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.

另一方面,一旦您尝试MPMoviePlayerController使用自己的功能扩展功能,代码很快就会变得讨厌 - 例如,您可能会开始使用多个计时器来覆盖诸如适当的饥饿检测之类的事情(实际上,该功能已包含在内)进入这个类的 iOS5 版本),自定义 UI 更新,......或者你可能最终有多个状态属性试图覆盖诸如在播放器仍在预缓冲时优雅地中止播放之类的事情。



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

我已经使用了这两个,我将继续使用这两个,这取决于我乐于构建的应用程序的需求。对于大多数(简单)项目,我建议使用MPMoviePlayerControllerover,AVPlayer因为它使用起来非常简单,只需几行代码,您就可以获得一个成熟的媒体播放器。如果您对媒体播放的要求更简单,请看一看MPMoviePlayerViewController(注意查看部分)。