测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2017年12月 |
由Casa Taloyum维护。
依赖 | |
AFNetworking | >= 0 |
CTPersistance | >= 0 |
HandyFrame | >= 0 |
pod update
然后打开CTVideoPlayerView.xcworkspace
来尝试演示。UIView<CTVideoPlayerDownloadingViewProtocol>
并将其分配给CTVideoView.downloadingView
即可。详情请参考DownloadThenPlayViewController
。待办事项
pod "CTVideoPlayerView"
import <CTVideoPlayerView/CTVideoViewCommonHeader.h>
CTVideoView *videoView = [[CTVideoView alloc] init];
videoView.assetToPlay = [AVURLAsset assetWithURL:assetUrl];
[videoView play];
CTVideoView可以直接播放任何AVAsset,但videoUrlType
和actualVideoUrlType
将设置为CTVideoViewVideoUrlTypeAsset
。
如果您关心所播放内容的URL类型,请参考下方的“使用URL播放”。
将videoUrl
设置为使CTVideoView
使用URL播放,videoUrlType
和actualVideoUrlType
将被适当地赋值。如果您不关心这些,请直接使用上述“使用媒体播放”。
简而言之
CTVideoView *videoView = [[CTVideoView alloc] init];
videoView.frame = CGRectMake(0,0,100,100);
[self.view addSubview:videoView];
videoView.videoUrl = [NSURL URLWithString:@"http://7xs8ft.com2.z0.glb.qiniucdn.com/rcd_vid_865e1fff817746d29ecc4996f93b7f74"]; // mp4 playable
[videoView play];
长篇大论
@interface SingleVideoViewController ()
@property (nonatomic, strong) CTVideoView *videoView;
@end
@implementation SingleVideoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.videoView];
// self.videoView.videoUrl = [NSURL URLWithString:@"http://7xs8ft.com2.z0.glb.qiniucdn.com/rcd_vid_865e1fff817746d29ecc4996f93b7f74"]; // mp4 playable
// self.videoView.videoUrl = [NSURL URLWithString:@"https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8"]; // m3u8 playable
self.videoView.videoUrl = [[NSBundle mainBundle] URLForResource:@"a" withExtension:@"mp4"]; // native url playable
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.videoView.frame = self.view.bounds;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.videoView play];
}
- (CTVideoView *)videoView
{
if (_videoView == nil) {
_videoView = [[CTVideoView alloc] init];
}
return _videoView;
}
@end
将下载策略设置为CTVideoViewDownloadStrategyDownloadOnlyForeground
或CTVideoViewDownloadStrategyDownloadForegroundAndBackground
以启用下载
[CTVideoManager sharedInstance].downloadStrategy = CTVideoViewDownloadStrategyDownloadForegroundAndBackground;
// they works the same
[videoView startDownloadTask];
// [[CTVideoManager sharedInstance] startDownloadTaskWithUrl:url];
// [[CTVideoManager sharedInstance] startAllDownloadTask];
如果下载策略是CTVideoViewDownloadStrategyNoDownload
,则即使进行下载调用,也不会生成下载任务。
[CTVideoManager sharedInstance].downloadStrategy = CTVideoViewDownloadStrategyNoDownload;
[videoView startDownloadTask]; // won't start download task
[[CTVideoManager sharedInstance] startDownloadTaskWithUrl:url]; // won't start download task
[[CTVideoManager sharedInstance] startAllDownloadTask]; // won't start download task
视频下载后,CTVideoPlayerView将记住本地文件所在位置以及哪个远程URL与之对应。您可以调用refreshUrl
来刷新当前视频视图的URL,然后播放本地视频文件。如果您创建一个全新的视频播放器视图,并将videoUrl
设置为远程URL,视频播放器视图将自动搜索本地文件并替换。
您可以设置 downloadDelegate
到视频播放器视图以获得更精确的控制。
@protocol CTVideoViewDownloadDelegate <NSObject>
@optional
- (void)videoViewWillStartDownload:(CTVideoView *)videoView;
- (void)videoView:(CTVideoView *)videoView downloadProgress:(CGFloat)progress;
- (void)videoViewDidFinishDownload:(CTVideoView *)videoView;
- (void)videoViewDidFailDownload:(CTVideoView *)videoView;
- (void)videoViewDidPausedDownload:(CTVideoView *)videoView;
- (void)videoViewDidDeletedDownloadTask:(CTVideoView *)videoView;
- (void)videoViewIsWaitingForDownload:(CTVideoView *)videoView;
@end
如果您没有视频播放器视图实例,您可以通过在 CTVideoViewDefinitions.h
中观察通知来获取下载事件的警报。
/**
* notifications
*/
extern NSString * const kCTVideoManagerWillDownloadVideoNotification;
extern NSString * const kCTVideoManagerDidFinishDownloadVideoNotification;
extern NSString * const kCTVideoManagerDownloadVideoProgressNotification;
extern NSString * const kCTVideoManagerDidFailedDownloadVideoNotification;
extern NSString * const kCTVideoManagerDidPausedDownloadVideoNotification;
extern NSString * const kCTVideoManagerDidDeletedDownloadVideoNotification;
/**
* notification userinfo keys
*/
extern NSString * const kCTVideoManagerNotificationUserInfoKeyRemoteUrlList;
extern NSString * const kCTVideoManagerNotificationUserInfoKeyRemoteUrl;
extern NSString * const kCTVideoManagerNotificationUserInfoKeyNativeUrl;
extern NSString * const kCTVideoManagerNotificationUserInfoKeyProgress;
只需使用 CTVideoDataCenter
。
@interface CTVideoDataCenter : NSObject
// create
- (void)insertRecordWithRemoteUrl:(NSURL *)remoteUrl status:(CTVideoRecordStatus)status;
// read
- (NSURL *)nativeUrlWithRemoteUrl:(NSURL *)remoteUrl;
- (NSArray <id<CTPersistanceRecordProtocol>> *)recordListWithStatus:(CTVideoRecordStatus)status;
- (CTVideoRecordStatus)statusOfRemoteUrl:(NSURL *)remoteUrl;
- (id<CTPersistanceRecordProtocol>)recordOfRemoteUrl:(NSURL *)url;
// update
- (void)updateWithRemoteUrl:(NSURL *)remoteUrl nativeUrl:(NSURL *)nativeUrl;
- (void)updateWithRemoteUrl:(NSURL *)remoteUrl nativeUrl:(NSURL *)nativeUrl status:(CTVideoRecordStatus)status;
- (void)updateStatus:(CTVideoRecordStatus)status toRemoteUrl:(NSURL *)remoteUrl;
- (void)updateStatus:(CTVideoRecordStatus)status progress:(CGFloat)progress toRemoteUrl:(NSURL *)remoteUrl;
- (void)updateAllStatus:(CTVideoRecordStatus)status;
- (void)pauseAllRecordWithCompletion:(void(^)(void))completion;
- (void)pauseRecordWithRemoteUrlList:(NSArray *)remoteUrlList completion:(void(^)(void))completion;
- (void)startDownloadAllRecordWithCompletion:(void(^)(void))completion;
- (void)startDownloadRemoteUrlList:(NSArray *)remoteUrlList completion:(void(^)(void))completion;
// delete
- (void)deleteWithRemoteUrl:(NSURL *)remoteUrl;
- (void)deleteAllRecordWithCompletion:(void(^)(NSArray *deletedList))completion;
- (void)deleteAllNotFinishedVideo;
@end
您可能希望在这个数据中心有更多方法,您可以发起一个issue告诉我您需要什么方法,或者直接给我发送pull request。
shouldObservePlayTime
设置为 YES,并设置 timeGapToObserve。如果 timeGapToObserve 是 1,表示 1/100 秒。
[videoView setShouldObservePlayTime:YES withTimeGapToObserve:10.0f]; // calls the delegate method `- (void)videoView:didPlayToSecond:` every 0.1s during playing.
timeDelegate
videoView.timeDelegate = self;
- (void)videoView:didPlayToSecond:
- (void)videoView:(CTVideoView *)videoView didPlayToSecond:(CGFloat)second
{
NSLog(@"%f", second);
}
videoView.playButton = customizedPlayButton;
videoView.retryButton = customizedRetryButton;
id<CTVideoViewButtonDelegate>
并实现方法来自定义按钮布局如果您不做这个,按钮将按照 CGSizeMake(100, 60) 的大小进行布局,并放置在视频中央。
使用 pod "HandyFrame"
将使您的布局代码更简单、更整洁。
#import <HandyFrame/UIView+LayoutMethods.h>
设置按钮代理
videoView.buttonDelegate = self;
使用 HandyFrame 进行布局
- (void)videoView:(CTVideoView *)videoView layoutPlayButton:(UIButton *)playButton
{
playButton.size = CGSizeMake(100, 60);
[playButton rightInContainer:5 shouldResize:NO];
[playButton bottomInContainer:5 shouldResize:NO];
}
- (void)videoView:(CTVideoView *)videoView layoutRetryButton:(UIButton *)retryButton
{
retryButton.size = CGSizeMake(100, 60);
[retryButton rightInContainer:5 shouldResize:NO];
[retryButton bottomInContainer:5 shouldResize:NO];
}
if (self.videoView.isFullScreen) {
[self.videoView exitFullScreen];
} else {
[self.videoView enterFullScreen];
}
此功能默认启用,如果您不想使用它,只需将 isSlideFastForwardDisabled
设置为 YES
videoView.isSlideFastForwardDisabled = YES;
要显示移动指示器,您应该设置 playControlDelegate
,并使用以下方法
@protocol CTVideoViewPlayControlDelegate <NSObject>
@optional
- (void)videoViewShowPlayControlIndicator:(CTVideoView *)videoView;
- (void)videoViewHidePlayControlIndicator:(CTVideoView *)videoView;
- (void)videoView:(CTVideoView *)videoView playControlDidMoveToSecond:(CGFloat)second direction:(CTVideoViewPlayControlDirection)direction;
@end
代理方法 - (void)videoViewShowPlayControlIndicator:(CTVideoView *)videoView;
告诉您,您可以显示自己的自定义指示器视图。
代理方法 - (void)videoViewHidePlayControlIndicator:(CTVideoView *)videoView;
告诉您,您可以隐藏自己的自定义指示器视图。
代理方法 - (void)videoView:(CTVideoView *)videoView playControlDidMoveToSecond:(CGFloat)second direction:(CTVideoViewPlayControlDirection)direction;
告诉您可以使用什么数据来更新您自己的自定义指示器视图的内容。
设置视频为静音
如果您希望在视频准备后就立即播放视频,请将此设置为 YES。
如果调用 play
而不是 prepare
,即使将此属性设置为 NO,视频在准备完成后也将播放。
设置为 YES 将在视频到达结束时重新播放视频。
设置为 YES 将会自动更改视频视图的朝向以适应视频播放。
这是一个只读属性,如果您想更改此值,请将 NSUserDefaults
中的 kCTVideoViewShouldDownloadWhenNotWifi
的布尔值设置为更改此值,默认是 NO。
设置为 YES 以指示视频视图应该显示操作按钮
自定义播放按钮的视图
重试按钮的视图
显示视频长度(秒)
如果您想触发 id<CTVideoViewTimeDelegate>
的 - (void)videoView:didPlayToSecond:
,您应该将此属性设置为 YES。
设置为 2.0 表示 2x 的速度。
设置为 YES 以指示视频视图应该显示视频封面视图
自定义封面视图