videoWithFlash.js 6.9 KB
/**
 *
 */



var client;    //客户端
var pubResultObj; // 存储底层给我的推流结果

var publishObj;

var videoWithFlash={};

window.videoWithFlash=videoWithFlash;

videoWithFlash.flashInit=function (_client){
    client=_client;

};

function teaTimeStamp() {
    var _teatimestamp= Date.parse(new Date());
    return _teatimestamp;
}
$(function () {

    //根据传进来的消息,创建老师NativeVideo窗口,进行播流
    client.on(MessageTypes.VIDEO_UPDATE,_videoUpdate);

    // //学生视频按钮点击
    // $("#stuVideo-video").click(function (e) {
    //     console.log("teacherVideo-video点击");
    //     $("#stuVideo-video").html(" ");
    //     $("#stuVideo-video").removeClass("stuVideo-video");
    //     $("#stuVideo-video").addClass("stuVideo-videoBg");
    //
    //
    //     $("#stuVideo-voice").html("");
    //     $("#stuVideo-voice").addClass("stuVideo-voice");
    //     $("#stuVideo-voice").removeClass("stuVideo-voiceBg");
    //
    //
    //     //获取推流地址
    //     achieveStupPushStream();
    //
    //
    // });
    //
    // //学生音频按钮点击
    // $("#stuVideo-voice").click(function (e) {
    //
    //     console.log("stuVideo-voice点击");
    //     $("#stuVideo-video").html("");
    //     $("#stuVideo-video").removeClass("stuVideo-videoBg");
    //     $("#stuVideo-video").addClass("stuVideo-video");
    //
    //     $("#stuVideo-voice").html("");
    //     $("#stuVideo-voice").removeClass("stuVideo-voice");
    //     $("#stuVideo-voice").addClass("stuVideo-voiceBg");
    // });

    //老师视频按钮点击
    $("#teacherVideo-video").click(function (e) {
        console.log("teacherVideo-video点击");
        $("#teacherVideo-video").html(" ");
        $("#teacherVideo-video").removeClass("teacherVideo-video");
        $("#teacherVideo-video").addClass("teacherVideo-videoBg");


        $("#teacherVideo-voice").html("");
        $("#teacherVideo-voice").addClass("teacherVideo-voice");
        $("#teacherVideo-voice").removeClass("teacherVideo-voiceBg");
        $(".teacherVideoBg").css("display","none");

        $(".teacherVideo").css("background", "#CCCCCC");
        $(".teacherVideo-voice").css("bottom","4px");

        //获取推流地址
        achieveTeaStream();

    });

    //老师音频按钮点击
    $("#teacherVideo-voice").click(function (e) {


        console.log("teacherVideo-voice点击");
        $("#teacherVideo-video").html("");
        $("#teacherVideo-video").removeClass("teacherVideo-videoBg");
        $("#teacherVideo-video").addClass("teacherVideo-video");

        $("#teacherVideo-voice").html("");
        $("#teacherVideo-voice").removeClass("teacherVideo-voice");
        $("#teacherVideo-voice").addClass("teacherVideo-voiceBg");
    });

});

    //获取推流地址
    function achieveTeaStream(){
        $(".publish").css("display","block");
        $(".play").css("display","none");


        var paramInfo={
            "type": "flash"
        };
        var pubResult=client.getVideoPublishPath(paramInfo);

        console.log("执行getPublishVideoPath,返回的结果",pubResult);




        //code为0,代表成功
        if (pubResult.code==0){


            if(! window.publishflash) {
            flashvideo(
                'publish',
                '100%',
                '100%',
                function (error, publishflash) {
                    if (error) {
                        return console.error(error);
                    }
                    window.publishflash = publishflash;
                    window.publishflashVideoId = teaTimeStamp();

                   publishURL(pubResult.publishUrl);
                },
                function (msg) {

                    console.log(' [publish] native 2 js', msg);
                    var _msg=JSON.parse(msg);
                    console.log("_msg===========>",_msg);
                    if(_msg.data.result == 0){
                        debugger;
                        //将之前底层取的推流结果传递给底层
                        var _successErrorResult= client.publishVideo(pubResult);
                        console.log("_successErrorResult======>",_successErrorResult)
                        if(_successErrorResult.code==0){
                            console.log("底层广播推流成功");
                        } else if(_successErrorResult.code==1){
                            console.log("底层广播推流失败,关闭推流");

                        }

                    }else {
                        console.error('publish error');
                    }
                }
            )

            }
            else {
                publishURL(pubResult.publishUrl);
            }

        }



    }
function publishURL(rtmpURL) {
    const msg = JSON.stringify({
        type: 10,
        videoId: publishflashVideoId,
        data: {
            url: rtmpURL
        }
    });
    console.log(publishflash._js2native(msg));
}

   // 监听当前是否有视频流更新
   function _videoUpdate(callbackDate){
       playCallbackDate=callbackDate;
        console.log("videoUpdate==============>",playCallbackDate);


    if ((callbackDate.status==1)&&(callbackDate.channelId!=='')){

        achievePlayStream(playCallbackDate);





    }

}

 //获取播流地址
 function achievePlayStream(_pubResultObj){
       console.log("_pubResultObj==========>",_pubResultObj)
     var _paramInfo={
         "type": "rtmp",
         "channelId": _pubResultObj.channelId,//频道号
         "classId": _pubResultObj.classId,//课堂号
         "siteId": _pubResultObj.siteId,//站点号
         "timestamp":_pubResultObj.timestamp,//时间戳
         "userId": _pubResultObj.userId,//userId
            };
     var streamInfo=client.getVideoPlayPath(_paramInfo);
     console.log("result是=============>",streamInfo);

     $(".teacherVideoBg").css("display","none");

     $(".teacherVideo").css("background", "#CCCCCC");
     $(".teacherVideo-voice").css("bottom","4px");
     // $(".publish").css("display","none");

     if(! window.playflash) {
         flashvideo(
             'play',
             '100%',
             '100%',
             function createPlayReady(error, playflash) {
                 if (error) {
                     return console.error(error);
                 }
                 window.playflash = playflash;
                 window.playflashVideoId = teaTimeStamp();

                 playURL(streamInfo.playUrl);
             },
             function (msg) {
                 console.log(' [play] native 2 js', msg);
             }
         )
     }
     else {
         playURL(streamInfo.playUrl);
     }
 }

 function playURL(rtmpURL) {
     const msg = JSON.stringify({
         type: 2,
         videoId: playflashVideoId,
         data: {
             url: rtmpURL
             // url:'rtmp://123.56.205.116:1935/live/alexwang'
         }
     });
     console.log(playflash._js2native(msg));
 }