版本記錄
| 版本號 | 時間 |
|---|---|
| V1.0 | 2017.12.30 |
前言
iOS系統(tǒng)中有很多方式可以播放視頻文件,這里我們就詳細的說明下播放視頻文件的原理和實例。希望能幫助到大家,大家能喜歡。
MediaPlayer框架
先看一下蘋果文檔中對該框架的描述。

這個框架就比較老了,用它里面的類MPMoviePlayerViewController就可以播放視頻,使用它播放視頻的優(yōu)點就是,代碼簡單;缺點就是界面都已經(jīng)系統(tǒng)化,感覺很low,不能滿足市場上對視頻播放器的需求,不過還好,蘋果提供了類MPMoviePlayerController,我們可以自定義UI皮膚,在GitHub上大家也可以找到KRVideoPlayer,它就是基于MPMoviePlayerController的播放器。
簡單的看一下,MediaPlayer框架的API接口。
//
// MediaPlayer.h
// MediaPlayer
//
// Copyright 2008-2011 Apple, Inc. All rights reserved.
//
#import <MediaPlayer/MediaPlayerDefines.h>
#import <TargetConditionals.h>
#if TARGET_OS_IPHONE
#import <MediaPlayer/MPMediaItemCollection.h>
#import <MediaPlayer/MPMediaLibrary.h>
#import <MediaPlayer/MPMediaPickerController.h>
#import <MediaPlayer/MPMediaPlaylist.h>
#import <MediaPlayer/MPMediaQuery.h>
#import <MediaPlayer/MPMediaQuerySection.h>
#import <MediaPlayer/MPMoviePlayerController.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
#import <MediaPlayer/MPMusicPlayerApplicationController.h>
#import <MediaPlayer/MPMusicPlayerController.h>
#import <MediaPlayer/MPMusicPlayerQueueDescriptor.h>
#import <MediaPlayer/MPPlayableContentDataSource.h>
#import <MediaPlayer/MPPlayableContentDelegate.h>
#import <MediaPlayer/MPPlayableContentManager.h>
#import <MediaPlayer/MPPlayableContentManagerContext.h>
#import <MediaPlayer/MPVolumeSettings.h>
#import <MediaPlayer/MPVolumeView.h>
#import <MediaPlayer/NSUserActivity+MediaPlayerAdditions.h>
#endif
#import <MediaPlayer/AVFoundation+MPNowPlayingInfoLanguageOptionAdditions.h>
#import <MediaPlayer/MPContentItem.h>
#import <MediaPlayer/MPError.h>
#import <MediaPlayer/MPMediaItem.h>
#import <MediaPlayer/MPNowPlayingInfoCenter.h>
#import <MediaPlayer/MPNowPlayingInfoLanguageOption.h>
#import <MediaPlayer/MPRemoteCommand.h>
#import <MediaPlayer/MPRemoteCommandCenter.h>
#import <MediaPlayer/MPRemoteCommandEvent.h>
#import <MediaPlayer/MPRemoteControlTypes.h>
注意:1.
MPMoviePlayerController在iOS9被Apple廢棄了;2. 當我們的需求需要深度定制播放器的時候,MPMoviePlayerController就顯得力不從心了。
AVFoundation框架
這個是很經(jīng)典的音視頻播放框架,大家應該都很熟悉的。
下面先看一下簡單的API。
/*
File: AVFoundation.h
Framework: AVFoundation
Copyright 2010-2015 Apple Inc. All rights reserved.
To report bugs, go to: http://developer.apple.com/bugreporter/
*/
#import <TargetConditionals.h>
#if TARGET_OS_WATCH
#if ! __has_include(<AVFoundation/AVAnimation.h>)
#define AVF_IS_WATCHOS_SDK 1
#endif
#endif
#import <AVFoundation/AVBase.h>
#if ! AVF_IS_WATCHOS_SDK
#import <AVFoundation/AVAnimation.h>
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetCache.h>
#import <AVFoundation/AVAssetExportSession.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <AVFoundation/AVAssetReader.h>
#import <AVFoundation/AVAssetReaderOutput.h>
#import <AVFoundation/AVAssetResourceLoader.h>
#import <AVFoundation/AVContentKeySession.h>
#import <AVFoundation/AVAssetTrack.h>
#import <AVFoundation/AVAssetTrackGroup.h>
#import <AVFoundation/AVAssetTrackSegment.h>
#import <AVFoundation/AVAssetWriter.h>
#import <AVFoundation/AVAssetWriterInput.h>
#import <AVFoundation/AVAsynchronousKeyValueLoading.h>
#import <AVFoundation/AVAudioMix.h>
#import <AVFoundation/AVAudioProcessingSettings.h>
#if TARGET_OS_IPHONE
#import <AVFoundation/AVAssetDownloadTask.h>
#import <AVFoundation/AVAssetDownloadStorageManager.h>
#endif
#if (TARGET_OS_IPHONE || defined(__MAC_10_7))
#import <AVFoundation/AVCaptureDataOutputSynchronizer.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVCaptureInput.h>
#import <AVFoundation/AVCaptureOutput.h>
#import <AVFoundation/AVCaptureSession.h>
#import <AVFoundation/AVCaptureSystemPressure.h>
#import <AVFoundation/AVCaptureVideoPreviewLayer.h>
#endif
#import <AVFoundation/AVComposition.h>
#import <AVFoundation/AVCompositionTrack.h>
#import <AVFoundation/AVCompositionTrackSegment.h>
#import <AVFoundation/AVDepthData.h>
#import <AVFoundation/AVError.h>
#endif
#import <AVFoundation/AVFAudio.h>
#if ! AVF_IS_WATCHOS_SDK
#import <AVFoundation/AVMediaFormat.h>
#import <AVFoundation/AVMediaSelection.h>
#import <AVFoundation/AVMediaSelectionGroup.h>
#import <AVFoundation/AVMetadataFormat.h>
#import <AVFoundation/AVMetadataIdentifiers.h>
#import <AVFoundation/AVMetadataItem.h>
#import <AVFoundation/AVMetadataObject.h>
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
#import <AVFoundation/AVMovie.h>
#import <AVFoundation/AVMovieTrack.h>
#endif
#import <AVFoundation/AVOutputSettingsAssistant.h>
#import <AVFoundation/AVPlayer.h>
#import <AVFoundation/AVPlayerItem.h>
#import <AVFoundation/AVPlayerItemMediaDataCollector.h>
#import <AVFoundation/AVPlayerItemOutput.h>
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
#import <AVFoundation/AVPlayerItemProtectedContentAdditions.h>
#endif
#import <AVFoundation/AVPlayerItemTrack.h>
#import <AVFoundation/AVPlayerLayer.h>
#import <AVFoundation/AVPlayerLooper.h>
#import <AVFoundation/AVPlayerMediaSelectionCriteria.h>
#import <AVFoundation/AVQueuedSampleBufferRendering.h>
#import <AVFoundation/AVRouteDetector.h>
#import <AVFoundation/AVSampleBufferAudioRenderer.h>
#import <AVFoundation/AVSampleBufferDisplayLayer.h>
#import <AVFoundation/AVSampleBufferRenderSynchronizer.h>
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
#import <AVFoundation/AVSampleBufferGenerator.h>
#import <AVFoundation/AVSampleCursor.h>
#endif
#import <AVFoundation/AVSynchronizedLayer.h>
#import <AVFoundation/AVTextStyleRule.h>
#import <AVFoundation/AVTime.h>
#import <AVFoundation/AVTimedMetadataGroup.h>
#import <AVFoundation/AVUtilities.h>
#import <AVFoundation/AVVideoCompositing.h>
#import <AVFoundation/AVVideoComposition.h>
#import <AVFoundation/AVVideoSettings.h>
#endif
#if TARGET_OS_TV
#import <AVFoundation/AVDisplayCriteria.h>
#endif
下面我們就看一下開發(fā)文檔中的說明。

關(guān)于AVFoudation框架的基礎(chǔ)理論,請參考我以前寫的那幾篇。
1. AVFoundation框架解析(一)—— 基本概覽
2. AVFoundation框架解析(二)—— 實現(xiàn)視頻預覽錄制保存到相冊
3. AVFoundation框架解析(三)—— 幾個關(guān)鍵問題之關(guān)于框架的深度概括
4. AVFoundation框架解析(四)—— 幾個關(guān)鍵問題之AVFoundation探索(一)
5. AVFoundation框架解析(五)—— 幾個關(guān)鍵問題之AVFoundation探索(二)
6. AVFoundation框架解析(六)—— 視頻音頻的合成(一)
7. AVFoundation框架解析(七)—— 視頻組合和音頻混合調(diào)試
8. AVFoundation框架解析(八)—— 優(yōu)化用戶的播放體驗
9. AVFoundation框架解析(九)—— AVFoundation的變化(一)
10. AVFoundation框架解析(十)—— AVFoundation的變化(二)
11. AVFoundation框架解析(十一)—— AVFoundation的變化(三)
12. AVFoundation框架解析(十二)—— AVFoundation的變化(四)
13. AVFoundation框架解析(十三)—— 構(gòu)建基本播放應用程序
14. AVFoundation框架解析(十四)—— VAssetWriter和AVAssetReader的Timecode支持(一)
AVKit框架
這個框架大家再熟悉不過了。

AVKit是建立在AVFoundation上,解決視頻播放的高級對象的框架,具體如下圖所示。

下面介紹幾個基于AVKit的視頻框架。
下面我們簡單看一下API接口。
/*
File: AVKit.h
Framework: AVKit
Copyright ? 2013-2016 Apple Inc. All rights reserved.
To report bugs, go to: http://developer.apple.com/bugreporter/
*/
#import <AVKit/AVKitDefines.h>
#if TARGET_OS_IPHONE
#import <AVKit/AVError.h>
#import <AVKit/AVPictureInPictureController.h>
#import <AVKit/AVPlayerViewController.h>
#import <AVKit/AVRoutePickerView.h>
#else
#import <AVKit/AVCaptureView.h>
#import <AVKit/AVPlayerView.h>
#endif // TARGET_OS_IPHONE
MobileVLCKit
AVKit盡管非常優(yōu)秀了,但是還是有它無能為力的地方,比如說,AVKit不能播放RMVB等非常規(guī)格式的視頻。但是MobileVLCKit就可以,它幾乎可以播放所有格式的視頻。
它支持下面的格式

如果你懂得一些視頻解碼更專業(yè)的知識,你還可以使用
ffmpeg和live555,這樣可以更加底層的學習視頻相關(guān)的技術(shù)。其實MobileVLCKit就是基于ffmpeg二次開發(fā)的。
下面給出該框架的靜態(tài)庫地址 - MobileVLCKit iOS nightly builds for ARMv7, ARMv7s, AArch64, x86_64 and i686
參考文章
后記
未完,待續(xù)~~~
