-
Notifications
You must be signed in to change notification settings - Fork 146
rapidIntegration
将金山云SDK解压后,将framework目录下KSYMediaPlayer.framework复制到项目下,选择需要集成的target,在target->Build Phases->Link Binary With Libraries下,将KSYMediaPlayer.framework添加进去,然后添加另外四个系统库:
- VideoToolbox.framework
- libstdc++.6.tbd 或者libstdc++.6.dylib
- libbz2.tbd 或者 libbz2.dylib
- libz.tbd 或者 libz.dylib
打开需要集成播放视频功能的视图源码,把如下代码复制并粘贴到你将播放视频的位置,例如到播放/停止按钮的方法中。
-
导入头文件
#import <KSYMediaPlayer/KSYMediaPlayer.h>
```
2. 初始化player
初始化需要几个步骤:
* 准备需要视频播放的UIView;
* 新建player;
* 设置url;
* 调用[prepareToPlay](#https://ksvc.github.io/KSYMediaPlayer_iOS/doc/html/Protocols/KSYMediaPlayback.html)开始播放。由于已经设置[shouldAutoplay](#shouldAutoplay)为TRUE,则prepare完成后立即开始播放。
```
KSYMoviePlayerController *_player; UIView *videoView; _videoView = [[UIView alloc] init]; _videoView.frame = CGRectMake( 0, 0, self.view.bounds.size.width, self.view.bounds.size.height); _videoView.backgroundColor = [UIColor lightGrayColor]; _player = KSYMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"http://121.42.58.232:8980/hls_test/1.m3u8"; _player.controlStyle = MPMovieControlStyleNone; [_player.view setFrame: _videoView.bounds]; // player's frame must match parent's [_videoView addSubview: _player.view]; _videoView.autoresizesSubviews = TRUE; _player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; _player.shouldAutoplay = TRUE; _player.scalingMode = MPMovieScalingModeAspectFit; [_player prepareToPlay]; ``` 说明:本SDK中不带任何播放控件,controlStyle只能设置为MPMovieControlStyleNone。
-
设置监听
当前SDK提供了多个Notificaton监听,具体可参考消息通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMediaPlaybackIsPreparedToPlayDidChangeNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMoviePlayerPlaybackStateDidChangeNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMoviePlayerPlaybackDidFinishNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMoviePlayerLoadStateDidChangeNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMovieNaturalSizeAvailableNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMoviePlayerFirstVideoFrameRenderedNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMoviePlayerFirstAudioFrameRenderedNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMoviePlayerSuggestReloadNotification) object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handlePlayerNotify:) name:(MPMoviePlayerPlaybackStatusNotification) object:nil];
-
销毁播放器
播放器在stop中完成内存释放,new一次KSYMoviePlayerController,需要stop一次。如果两者未配对调用,将引发内存泄露。
[_player stop]