概述
ijkplayer 是Bilibili開發(fā)并開源的輕量級視頻播放器,支持本地網(wǎng)絡視頻播放以及流媒體播放,支持iOS和Android平臺。ijkplayer基于 FFmpeg 是一套可以用來記錄、轉(zhuǎn)換數(shù)字音頻、視頻,并能將其轉(zhuǎn)化為流的開源計算機程序。 FFmpeg 采用LGPL或GPL許可證,提供了錄制、轉(zhuǎn)換以及流化音視頻的完整解決方案,包括了領(lǐng)先的音、視頻編碼庫libavcodec等。
特性
platform | version | CPU| video-output|audio-output|hw-decoder
:---:|:---:|:---:|:---:|:---:
iOS | iOS 7.0+ | armv7, arm64, i386, x86_64|OpenGL ES 2.0|AudioQueue, AudioUnit|VideoToolbox (iOS 8+)
Android | API 9+ | ARMv7a, ARM64v8a, x86 |NativeWindow, OpenGL ES 2.0|AudioTrack, OpenSL ES|MediaCodec (API 16+, Android 4.1+)
IJKMediaPlayback
IJKMediaPlayback是 ijkplayer 中最重要的協(xié)議。由于 ijkplayer 中有幾種不同類型的播放器實現(xiàn)(如:基于AVPlayer實現(xiàn)的IJKAVMoviePlayerController,基于MPMoviePlayerController實現(xiàn)的IJKMPMoviePlayerController,基于FFmpeg實現(xiàn)的IJKFFMoviePlayerController)。為了統(tǒng)一播放行為,ijkplayer 使用了一套基本協(xié)議,使每種底層不同實現(xiàn)的播放器能為使用者提供統(tǒng)一的接口。
@protocol IJKMediaPlayback <NSObject>
- (void)prepareToPlay;
- (void)play;
- (void)pause;
- (void)stop;
- (BOOL)isPlaying;
- (void)shutdown;
- (void)setPauseInBackground:(BOOL)pause;
// 視頻畫面
@property(nonatomic, readonly) UIView *view;
// 重播次數(shù)
@property(nonatomic) NSTimeInterval currentPlaybackTime;
@property(nonatomic, readonly) NSTimeInterval duration;
@property(nonatomic, readonly) NSTimeInterval playableDuration;
@property(nonatomic, readonly) NSInteger bufferingProgress;
@property(nonatomic, readonly) BOOL isPreparedToPlay;
@property(nonatomic, readonly) IJKMPMoviePlaybackState playbackState;
// 加載狀態(tài)
@property(nonatomic, readonly) IJKMPMovieLoadState loadState;
@property(nonatomic, readonly) int64_t numberOfBytesTransferred;
// 視頻原始尺寸
@property(nonatomic, readonly) CGSize naturalSize;
// 畫面填充模式
@property(nonatomic) IJKMPMovieScalingMode scalingMode;
@property(nonatomic) BOOL shouldAutoplay;
@property (nonatomic) BOOL allowsMediaAirPlay;
@property (nonatomic) BOOL isDanmakuMediaAirPlay;
@property (nonatomic, readonly) BOOL airPlayMediaActive;
@property (nonatomic) float playbackRate;
@property (nonatomic) float playbackVolume;
// 視頻截圖
- (UIImage *)thumbnailImageAtCurrentTime;
#pragma mark Notifications
#ifdef __cplusplus
#define IJK_EXTERN extern "C" __attribute__((visibility ("default")))
#else
#define IJK_EXTERN extern __attribute__((visibility ("default")))
#endif
// -----------------------------------------------------------------------------
// MPMediaPlayback.h
// Posted when the prepared state changes of an object conforming to the MPMediaPlayback protocol changes.
// This supersedes MPMoviePlayerContentPreloadDidFinishNotification.
IJK_EXTERN NSString *const IJKMPMediaPlaybackIsPreparedToPlayDidChangeNotification;
// -----------------------------------------------------------------------------
// MPMoviePlayerController.h
// Movie Player Notifications
// Posted when the scaling mode changes.
IJK_EXTERN NSString* const IJKMPMoviePlayerScalingModeDidChangeNotification;
// Posted when movie playback ends or a user exits playback.
IJK_EXTERN NSString* const IJKMPMoviePlayerPlaybackDidFinishNotification;
IJK_EXTERN NSString* const IJKMPMoviePlayerPlaybackDidFinishReasonUserInfoKey; // NSNumber (IJKMPMovieFinishReason)
// Posted when the playback state changes, either programatically or by the user.
IJK_EXTERN NSString* const IJKMPMoviePlayerPlaybackStateDidChangeNotification;
// Posted when the network load state changes.
IJK_EXTERN NSString* const IJKMPMoviePlayerLoadStateDidChangeNotification;
// Posted when the movie player begins or ends playing video via AirPlay.
IJK_EXTERN NSString* const IJKMPMoviePlayerIsAirPlayVideoActiveDidChangeNotification;
// -----------------------------------------------------------------------------
// Movie Property Notifications
// Calling -prepareToPlay on the movie player will begin determining movie properties asynchronously.
// These notifications are posted when the associated movie property becomes available.
IJK_EXTERN NSString* const IJKMPMovieNaturalSizeAvailableNotification;
// -----------------------------------------------------------------------------
// Extend Notifications
IJK_EXTERN NSString *const IJKMPMoviePlayerVideoDecoderOpenNotification;
IJK_EXTERN NSString *const IJKMPMoviePlayerFirstVideoFrameRenderedNotification;
IJK_EXTERN NSString *const IJKMPMoviePlayerFirstAudioFrameRenderedNotification;
IJK_EXTERN NSString *const IJKMPMoviePlayerDidSeekCompleteNotification;
IJK_EXTERN NSString *const IJKMPMoviePlayerDidSeekCompleteTargetKey;
IJK_EXTERN NSString *const IJKMPMoviePlayerDidSeekCompleteErrorKey;
IJK_EXTERN NSString *const IJKMPMoviePlayerDidAccurateSeekCompleteCurPos;
IJK_EXTERN NSString *const IJKMPMoviePlayerAccurateSeekCompleteNotification;
@end
IJKMediaUrlOpenDelegate
IJKMediaUrlOpenDelegate 在 IJKFFMoviePlayerController 使用,當 ijkplayer 打開HTTP、TCP等相關(guān)URL的時候會回調(diào) - (void)willOpenUrl:(IJKMediaUrlOpenData*) urlOpenData 方法。
@protocol IJKMediaUrlOpenDelegate <NSObject>
- (void)willOpenUrl:(IJKMediaUrlOpenData*) urlOpenData;
@end
IJKMediaNativeInvokeDelegate
IJKMediaNativeInvokeDelegate 在 IJKFFMoviePlayerController 使用,當FFmpeg 在處理相關(guān)事件(如:AVAPP_EVENT_WILL_HTTP_OPEN、AVAPP_EVENT_DID_HTTP_OPEN、AVAPP_EVENT_WILL_HTTP_SEEK、AVAPP_EVENT_DID_HTTP_SEEK)的時候回調(diào)代理的 - (int)invoke:(IJKMediaEvent)event attributes:(NSDictionary *)attributes 方法。
@protocol IJKMediaNativeInvokeDelegate <NSObject>
- (int)invoke:(IJKMediaEvent)event attributes:(NSDictionary *)attributes;
@end
總結(jié)
ijkplayer 里面的協(xié)議比較簡單,協(xié)議的數(shù)量也不是很多。ijkplayer 提供了幾種不同實現(xiàn)的播放器,雖然這幾種播放器的實現(xiàn)不同,但都提供了統(tǒng)一的接口。因此,我們并不需要關(guān)心實現(xiàn)細節(jié)。