The video player based on AVPlayer encapsulation has rich functions, fast integration and strong customization.
Execute the ExportFramework script in the project to automatically generate the framework
Cartfile
, list the frameworks you want to use in this filegithub "easyui/EZPlayer"
carthage update
, get dependencies to Carthage/Checkouts folder, build one by one/usr/local/bin/carthage copy-frameworks
$(SRCROOT)/Carthage/Build/iOS/EZPlayer.framework
Podfile
, list the frameworks you want to use in this fileproject 'EZPlayerExample.xcodeproj'
platform :ios, '8.0'
target '<Your Target Name>' do
use_frameworks!
pod 'EZPlayer'
end
Podfile
the next file directory$ pod install
func playEmbeddedVideo(mediaItem: MediaItem, embeddedContentView contentView: UIView? = nil , userinfo: [AnyHashable : Any]? = nil ) {
//stop
self.releasePlayer()
......
self.player!.backButtonBlock = { fromDisplayMode in
if fromDisplayMode == .embedded {
self.releasePlayer()
}else if fromDisplayMode == .fullscreen {
if self.embeddedContentView == nil && self.player!.lastDisplayMode != .float{
self.releasePlayer()
}
}else if fromDisplayMode == .float {
self.releasePlayer()
}
}
self.embeddedContentView = contentView
//self.embeddedContentView为nil时就是全屏播放
self.player!.playWithURL(mediaItem.url! , embeddedContentView: self.embeddedContentView)
}
//根据设备横置自动全屏
open var autoLandscapeFullScreenLandscape = UIDevice.current.userInterfaceIdiom == .phone
//指定全屏模式是竖屏还是横屏
open var fullScreenMode = EZPlayerFullScreenMode.landscape
//进去全屏模式
open func toFull(_ orientation:UIDeviceOrientation = .landscapeLeft, animated: Bool = true ,completion: ((Bool) -> Swift.Void)? = nil)
//进入嵌入屏模式
open func toEmbedded(animated: Bool = true , completion: ((Bool) -> Swift.Void)? = nil)
//进入浮动模式
open func toFloat(animated: Bool = true, completion: ((Bool) -> Swift.Void)? = nil)
Example: EZPlayerExample-DisplayMode
//自定义皮肤只要实现这两个协议
public protocol EZPlayerHorizontalPan: class {
func player(_ player: EZPlayer ,progressWillChange value: TimeInterval)
func player(_ player: EZPlayer ,progressChanging value: TimeInterval)
func player(_ player: EZPlayer ,progressDidChange value: TimeInterval)
}
public protocol EZPlayerGestureRecognizer: class {
func player(_ player: EZPlayer ,singleTapGestureTapped singleTap: UITapGestureRecognizer)
func player(_ player: EZPlayer ,doubleTapGestureTapped doubleTap: UITapGestureRecognizer)
}
//点击事件还可以接受通知
static let EZPlayerTapGestureRecognizer = Notification.Name(rawValue: "com.ezplayer.EZPlayerTapGestureRecognizer")
/// 支持airplay
open var allowsExternalPlayback = true
/// airplay连接状态
open var isExternalPlaybackActive: Bool
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
... MediaManager.sharedInstance.playEmbeddedVideo(url:URL.Test.localMP4_0, embeddedContentView: cell?.contentView)
//主要是设置indexPath和scrollView 属性
MediaManager.sharedInstance.player?.indexPath = indexPath
MediaManager.sharedInstance.player?.scrollView = tableView
}
//设置
open var videoGravity = EZPlayerVideoGravity.aspect
主要通过下面两个extension来设置,查看
AVAsset+EZPlayer.swift
//获取所有cc
public var closedCaption: [AVMediaSelectionOption]?
//获取所有subtitle
public var subtitles: [(subtitle: AVMediaSelectionOption,localDisplayName: String)]?
//获取所有audio
public var audios: [(audio: AVMediaSelectionOption,localDisplayName: String)]?
AVPlayerItem+EZPlayer.swift
/// 获取/设置当前subtitle/cc
public var selectedMediaCharacteristicLegibleOption:AVMediaSelectionOption?
/// 获取/设置当前cc
public var selectedClosedCaptionOption:AVMediaSelectionOption?
/// 获取/设置当前subtitle
public var selectedSubtitleOption:AVMediaSelectionOption?
/// 获取/设置当前audio
public var selectedMediaCharacteristicAudibleOption:AVMediaSelectionOption?
//不支持m3u8
open func generateThumbnails(times: [TimeInterval],maximumSize: CGSize, completionHandler: @escaping (([EZPlayerThumbnail]) -> Swift.Void ))
//支持m3u8
func snapshotImage() -> UIImage?
Player control skin customization (a set of floating skins, a set of skins for embedding and full screen)
There are three sets of skins for EZPlayer:
/// 嵌入模式的控制皮肤
open var controlViewForEmbedded : UIView?
/// 浮动模式的控制皮肤
open var controlViewForFloat : UIView?
/// 浮动模式的控制皮肤
open var controlViewForFullscreen : UIView?
When the default controlViewForFullscreen is empty, the controlViewForEmbedded skin will be used by default
You can set the controlViewForEmbedded skin when EZPlayer is initialized
public init(controlView: UIView? )
Example: EZPlayerExample-Skin(ad)
If you enter an advertisement during playback, you need to temporarily set the advertisement skin, and you can set the attributes:
open var controlViewForIntercept : UIView?
Example: EZPlayerExample-Skin(ad)
You can refer to the EZPlayerExample_RN project to use the latest swift4
and latest react native
version to encapsulate EZPlayer to realize most of the functions of EZPlayer.
EZRNPlayerView.swift: encapsulation of EZPlayer, docking with javascript
EZRNPlayerViewManager.swift: EZPlayer component manager
EZRNPlayerViewBridge.h & EZRNPlayerViewBridge.m : oc桥接
EZPlayer.js: js api encapsulated for EZPlayer
key | description | value |
---|---|---|
source | Video data source | PropTypes.object |
autoPlay | Play automatically after setting the data source | PropTypes.bool |
useDefaultUI | Use EZPlayer’s own skin | PropTypes.bool |
videoGravity | Video aspect ratio | PropTypes.string(aspect,aspectFill,scaleFill) |
fullScreenMode | Is the full screen mode vertical or horizontal | PropTypes.string(portrait,landscape) |
onPlayerHeartbeat | Player declaration cycle heartbeat | PropTypes.func |
onPlayerPlaybackTimeDidChange | trigger of addPeriodicTimeObserver method | PropTypes.func |
onPlayerStatusDidChange | Player status change | PropTypes.func |
onPlayerPlaybackDidFinish | End of video | PropTypes.func |
onPlayerLoadingDidChange | loading status change | PropTypes.func |
onPlayerControlsHiddenDidChange | The player control bar is hidden and displayed | PropTypes.func |
onPlayerDisplayModeDidChange | The player display mode has changed (full screen, embedded screen, floating) | PropTypes.object |
onPlayerDisplayModeChangedWillAppear | Player display mode animation start | PropTypes.func |
onPlayerDisplayModeChangedDidAppear | Player display mode animation ends | PropTypes.func |
onPlayerTapGestureRecognizer | Tap player gesture notification | PropTypes.func |
onPlayerDidPersistContentKey | FairPlay DRM | PropTypes.func |
function | description |
---|---|
play() | play |
pause() | pause |
stop() | end |
seek(time, callback) | Set playback progress, in seconds |
replaceToPlay(source) | Replace the playback source |
rate(rate) | Set playback rate |
autoPlay(autoPlay) | Set auto play, autoPlay is PropTypes.bool |
videoGravity(videoGravity) | Set the video aspect ratio, videoGravity: aspect, aspectFill, scaleFill |
toEmbedded(animated = true, callback) | Enter embedded screen mode |
toFloat(animated = true, callback) | Enter floating screen mode |
toFull(orientation =‘landscapeLeft’, animated = true, callback) | Enter full screen mode, orientation: landscapeLeft, landscapeRight |
fullScreenMode(fullScreenMode) | Set the full screen mode, fullScreenMode:portrait, landscape |
//Basic use//
BasePlayerExample.js
< EZPlayer
ref = { ( e ) => this . _EzPlayer = e }
style = { styles . Player }
source = { this . State . Source }
autoPlay={true}
videoGravity={'aspect'}
fullScreenMode={'landscape'}
/>
//自定义ui
//EZCustomPlayer.js
<TouchableWithoutFeedback onPress={this.action.onScreenTouch}>
<View>
<EZPlayer
{...this.props}
useDefaultUI={false}
ref={(nativePlayer) => this.player.ref = nativePlayer}
style={this.props.style}
onPlayerHeartbeat={this.events.onPlayerHeartbeat}
onPlayerPlaybackTimeDidChange={this.events.onPlayerPlaybackTimeDidChange}
onPlayerStatusDidChange={this.events.onPlayerStatusDidChange}
onPlayerPlaybackDidFinish={this.events.onPlayerPlaybackDidFinish}
onPlayerLoadingDidChange={this.events.onPlayerLoadingDidChange}
onPlayerControlsHiddenDidChange={this.events.onPlayerControlsHiddenDidChange}
onPlayerDisplayModeDidChange={this.events.onPlayerDisplayModeDidChange}
onPlayerDisplayModeChangedWillAppear={this.events.onPlayerDisplayModeChangedWillAppear}
onPlayerDisplayModeChangedDidAppear={this.events.onPlayerDisplayModeChangedDidAppear}
onPlayerTapGestureRecognizer={this.events.onPlayerTapGestureRecognizer}
onPlayerDidPersistContentKey={this.events.onPlayerDidPersistContentKey}
/>
{this.renderLoader()}
{this.renderBottomControls()}
</View>
</TouchableWithoutFeedback>
ps: react-native-ezplayer finishing
EZPlayer complies with the MIT protocol, please refer to MIT for details
Author: easyui
Source Code: https://github.com/easyui/EZPlayer
#react-native #react #mobile-apps