直播播放器使用

播放器功能特性

与系统播放器MPMoviePlayerController接口一致,可以无缝快速切换至KSYMediaPlayer;
本地全媒体格式支持, 并对主流的媒体格式(mp4, avi, wmv, flv, mkv, mov, rmvb 等 )进行优化;
支持广泛的流式视频格式, HLS, RTMP, HTTP Rseudo-Streaming 等;
更详细使用 查看 https://github.com/ksvc/KSYMediaPlayer_iOS

使用cocopods导入SDK

pod ‘Socket.IO-Client-Swift’
由于苹果上架要求 不得包含UIWebView 而项目中libksybase.a 包含UIWebView

解决方法:从github下载:https://github.com/ksvc/KSYMediaPlayer_iOS;
找到这个.a文件替换工程里的的,每次pod update都要重新替换一次。有点繁琐
其他方法 参考:https://ask.csdn.net/questions/1303736

//播放器
#import "libksygpulive/KSYMoviePlayerController.h"
//推流 暂未使用 
//#import 
//#import 


@interface LivePlayViewController ()

@property (strong, nonatomic) KSYMoviePlayerController *js_player;
@property (nonatomic,strong)UIView *videoPlayView;//视频播放view


@end

@implementation LivePlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

  self.videoPlayView = [[UIView alloc] initWithFrame: CGRectMake(0,0, _window_width, ScaleHeight(196))];
    self.videoPlayView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.videoPlayView];

    [self initLivePlaySDK];
}

- (void)initLivePlaySDK
{

// _url 拉流地址
    self.js_player =  [[KSYMoviePlayerController alloc] initWithContentURL: _url];
    self.js_player.view.backgroundColor = [UIColor clearColor];
    [self.js_player.view setFrame: self.videoPlayView.bounds];  // 播放器的frame 必须与父视图保持一致

// 当做全屏播放的时候 也需要设置frame
//        self.videoPlayView.frame = CGRectMake(0,0, _window_width, _window_height);
//        [self.js_player.view setFrame: _videoPlayView.bounds];  
    
    [self.videoPlayView addSubview: self.js_player.view];
    self.js_player.shouldAutoplay = TRUE;
    self.js_player.bInterruptOtherAudio = YES;
    self.js_player.shouldEnableKSYStatModule = TRUE;

//    MPMovieScalingModeNone,       // 无缩放
//    MPMovieScalingModeAspectFit,  // 同比适配,某个方向会有黑边
//    MPMovieScalingModeAspectFill, // 同比填充,某个方向的显示内容可能被裁剪
//    MPMovieScalingModeFill        // 满屏填充,与原始视频比例不一致
    self.js_player.scalingMode = MPMovieScalingModeFill;
    
// 这里后台做了防盗链 播放器相应添加 referer
    NSMutableDictionary *header = [[NSMutableDictionary alloc] init];
//    [header setObject:@"demo-user-agent" forKey:@"user-agent"];
//    [header setObject:@"just.demo.host" forKey:@"Host"];
    [header setObject:[NSString stringWithFormat:@"%@%@",kApiPrefix,@"/api/public/"] forKey:@"referer"];
    [self.js_player setHttpHeaders:header];
    
    [self.js_player prepareToPlay];
    [self.js_player setVolume:2.0 rigthVolume:2.0];
    self.js_player.videoDecoderMode = MPMovieVideoDecoderMode_Software;


    [self.js_player setLogBlock:^(NSString *logJson) {
        
        
        NSDictionary *dic = [weakSelf dictionaryWithJsonString:logJson];
        
        NSString *fail_codeStr = dic[@"play_stat"];

        int fail_code = [dic[@"fail_code"] intValue];;
        
        if ([fail_codeStr isEqualToString:@"fail"] && fail_code != 0) {
            
            NSLog(@"播放失败");

        }else if (![fail_codeStr isEqualToString:@"fail"] && fail_code == 0){
            
            NSLog(@"播放成功");
        }
        
    }];
    
 [self.js_player play]; // 播放
 [self.js_player stop];// 停止播放
 [self.js_player pause]; // 暂停播放



}
#pragma mark 注册通知
-(void)registnsNotifition{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
    
    
}
#pragma mark - 前后台 支持前后台播放  需要info.plist 配置
/*
 如何实现后台播放
 需要APP有后台执行权限,在工程Info.plist中添加后台运行模式,设置为audio。具体是添加UIBackgroundModes项,值为audio。
 当用户点击home按钮后,播放器进入后台继续读取数据并播放音频。
 当APP回到前台后,音频继续播放。图像渲染内容保持和音频同步。
 如果在开启后台运行模式后,需要实现切换到后台暂停音频,需要监听相关事件并主动调用pause操作。
 */

-(void)onAppDidEnterBackground {
    
    [self.js_player pause];
}
-(void)onAppWillEnterForeground {
    
    [self.js_player play];

}
@end

你可能感兴趣的:(直播播放器使用)