Skip to content

rapidIntegration

mayudong1 edited this page Nov 3, 2017 · 13 revisions

快速集成

如果项目中已经集成推流SDK,因为其中已经包含本SDK,则不需要再单独集成播放SDK,否则会有冲突

Step1 集成framework

将金山云SDK解压后,将framework/live(或framework/live_dy)目录下KSYMediaPlayer.framework复制到项目下,选择需要集成的target,在target->Build Phases->Link Binary With Libraries下,将KSYMediaPlayer.framework添加进去,然后添加另外四个系统库。
如果使用的是动态库(即live_dy或vod_dy目录下的framework)需要将framework添加到target->General->Embedded Binary下:

  • VideoToolbox.framework
  • libstdc++.6.tbd 或者libstdc++.6.dylib
  • libbz2.tbd 或者 libbz2.dylib
  • libz.tbd 或者 libz.dylib

framework集成

Step2 调用

打开需要集成播放视频功能的视图源码,把如下代码复制并粘贴到你将播放视频的位置,例如到播放/停止按钮的方法中。

  1. 导入头文件
#import <KSYMediaPlayer/KSYMediaPlayer.h>

如果已经集成推流SDK,则需要导入的头文件为如下头文件,其中已经包含了播放器的头文件

#import <libksygpulive/libksygpulive.h>
  1. 初始化player
初始化需要几个步骤:
  • 准备需要视频播放的UIView;

  • 新建player;

  • 设置url;

  • 调用prepareToPlay开始播放。由于已经设置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。

  1. 设置监听

    当前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];  
    
  2. 销毁播放器

    播放器在stop中完成内存释放,new一次KSYMoviePlayerController,需要stop一次。如果两者未配对调用,将引发内存泄露。

    [_player stop]
    
Clone this wiki locally