使用SGPlayer播放视频

/ 0评 / 7

之前的播放rtsp视频流用的是IJK,VR渲染使用的是MD360,但是经常出现断,丢包,表象就是花屏(原因暂时只是猜测),设置参数并不能得到好的改善,于是又找了一个播放器,能够播放视频、直播流,并且能够渲染VR效果,并且花屏现象稍有改观,来看看如何使用:

SGPlayer是一个开源的播放器,地址为:https://github.com/libobjc/SGPlayer

首先克隆下来,切换到所在目录,运行编译脚本,其实不是编译,只是把作者编译好的ffmpeg下载下来,也可以使用自己编译的ffmpeg,但是要先执行编译脚本,然后把ffmpeg库复制到对应的目录下 ,目录为: /SGPlayer/Classes/Core/SGFFPlayer/ffmpeg/lib-iOS

git clone https://github.com/libobjc/SGPlayer.git
cd SGPlayer
sh compile-build.sh iOS

然后运行项目,打成 framework 包,复制到我们的工程中,需要添加以下依赖

- CoreMedia.framework
- AudioToolBox.framework
- VideoToolBox.framework
- libiconv.tbd
- libbz2.tbd
- libz.tbd

来看使用方法

- (void)viewDidLoad {
    [super viewDidLoad];
    self.player = [SGPlayer player];
    self.player.volume = 0;
    self.player.view.frame = CGRectMake(0, 0, kDEVICE_WIDTH, kDEVICE_HEIGHT*2/5);
    [self.player registerPlayerNotificationTarget:self
                                      stateAction:@selector(stateAction:)
                                   progressAction:@selector(progressAction:)
                                   playableAction:@selector(playableAction:)
                                      errorAction:@selector(errorAction:)];
    [self.player setViewTapAction:^(SGPlayer * _Nonnull player, SGPLFView * _Nonnull view) {
        SRQLog(@"player display view did click!");
    }];
    [self.videoView addSubview:self.player.view];
    self.player.displayMode = SGDisplayModeNormal;
    self.player.viewGravityMode = SGGravityModeResizeAspectFill;
    self.player.decoder = [SGPlayerDecoder decoderByDefault];
   //可以自己配置硬解和ffmpeg参数
    self.player.decoder.hardwareAccelerateEnableForFFmpeg = NO;
    [self.player.decoder setFFmpegFormatContextOptionStringValue:@"tcp" forKey:@"rtsp_transport"];
    
    SRQLog(@"===-=-配置为=====%@=-=-",[self.player.decoder FFmpegFormatContextOptions]);
}

//播放器的通知
- (void)stateAction:(NSNotification *)notification{
    SGState * state = [SGState stateFromUserInfo:notification.userInfo];

    NSString * text;
    switch (state.current) {
        case SGPlayerStateNone:
            text = @"None";
            break;
        case SGPlayerStateBuffering:
            text = @"Buffering...";
            break;
        case SGPlayerStateReadyToPlay:
            text = @"Prepare";
            break;
        case SGPlayerStatePlaying:
            text = @"Playing";
            break;
        case SGPlayerStateSuspend:
            text = @"Suspend";
            break;
        case SGPlayerStateFinished:
            text = @"Finished";
            break;
        case SGPlayerStateFailed:
            text = @"Error";
            break;
    }
    SRQLog(@"=-=-=状态为--=--=%@=-=-=-",text);
}

- (void)progressAction:(NSNotification *)notification{
    SGProgress * progress = [SGProgress progressFromUserInfo:notification.userInfo];
    SRQLog(@"进度为-------%@-----",[self timeStringFromSeconds:progress.current]);
}

- (void)playableAction:(NSNotification *)notification{
    SGPlayable * playable = [SGPlayable playableFromUserInfo:notification.userInfo];
    SRQLog(@"playable time : %f", playable.current);
}

- (void)errorAction:(NSNotification *)notification{
    SGError * error = [SGError errorFromUserInfo:notification.userInfo];
    SRQLog(@"player did error : %@", error.error);
}

- (NSString *)timeStringFromSeconds:(CGFloat)seconds{
    return [NSString stringWithFormat:@"%ld:%.2ld", (long)seconds / 60, (long)seconds % 60];
}

评论已关闭。