CCMediaPlayer.js 7.21 KB
const player = "com/topdraw/component/CocosMediaPlayer";

const CALLBACK_FUNCS = {
    INFO: null,
    PREPARED: null,
    SEEKCOMPLETE: null,
    COMPLETION: null,
    ERROR: null,
    BUFFERSTART: null,
    BUFFERSTOP: null,
    TIMEOUT: null,
};
//根据春哥封装的视频接口,整合出来的视频播放组件
cc.Class({
    extends: cc.Component,

    properties: {
        url: '',
    },

    onLoad() {
        this._playing = false;
        this.init("Android");       //初始化工作,Android, NewTV, Vitamio, IJK
        this.setArea(0, 720 - 720, 1280, 720);      //默认全屏    
    },

    init: function (type) {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod(player, "jsInit", "(Ljava/lang/String;)V", type);
        }
    },
    setArea: function (x, y, w, h) {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod(player, "jsSetArea", "(IIII)V", x, y, w, h);
        } 
    },
    setPlayURL: function (url) {
        this._playing = false;
        this.url = url;       //fix jerry
        jsb.reflection.callStaticMethod(player, "jsSetPlayURL", "(Ljava/lang/String;)V", url);
    },
    setInfo: function (info) {
        jsb.reflection.callStaticMethod(player, "jsSetInfo", "(Ljava/lang/String;)V", info);
    },
    prepare: function () {
        jsb.reflection.callStaticMethod(player, "jsPrepare", "()V");
    },
    prepareAsync: function () {
        jsb.reflection.callStaticMethod(player, "jsPrepareAsync", "()V");
    },
    start: function (time) {
        this._playing = true;
        jsb.reflection.callStaticMethod(player, "jsStart", "(I)V", time);
    },
    pause: function () {
        this._playing = false;
        jsb.reflection.callStaticMethod(player, "jsPause", "()V");
    },
    seekTo: function (time) {
        jsb.reflection.callStaticMethod(player, "jsSeekTo", "(I)V", time);
    },
    setVolume: function (volume) {
        jsb.reflection.callStaticMethod(player, "jsSetVolume", "(I)V", volume);
    },
    setVolume: function (leftVolume, rightVolume) {
        jsb.reflection.callStaticMethod(player, "jsSetVolume", "(FF)V", leftVolume, rightVolume);
    },
    stop: function () {
        jsb.reflection.callStaticMethod(player, "jsStop", "()V");
    },
    reset: function () {
        jsb.reflection.callStaticMethod(player, "jsReset", "()V");
    },
    release: function () {
        CALLBACK_FUNCS.INFO = null;
        CALLBACK_FUNCS.PREPARED = null;
        CALLBACK_FUNCS.SEEKCOMPLETE = null;
        CALLBACK_FUNCS.COMPLETION = null;
        CALLBACK_FUNCS.ERROR = null;
        CALLBACK_FUNCS.BUFFERSTART = null;
        CALLBACK_FUNCS.BUFFERSTOP = null;
        CALLBACK_FUNCS.TIMEOUT = null;
        jsb.reflection.callStaticMethod(player, "jsRelease", "()V");
    },
    getCurrentPosition: function () {
        return jsb.reflection.callStaticMethod(player, "jsGetCurrentPosition", "()I");
    },
    getDuration: function () {
        return jsb.reflection.callStaticMethod(player, "jsGetDuration", "()I");
    },
    getVolume: function () {
        return jsb.reflection.callStaticMethod(player, "jsGetVolume", "()I");
    },
    isPlaying: function () {
        // return jsb.reflection.callStaticMethod(player, "jsIsPlaying", "()Z");
        return this._playing;
    },
    setOnInfoListener: function (func) {
        CALLBACK_FUNCS.INFO = func;
    },
    setOnPreparedListener: function (func) {
        CALLBACK_FUNCS.PREPARED = func;
    },
    setOnSeekCompleteListener: function (func) {
        CALLBACK_FUNCS.SEEKCOMPLETE = func;
    },
    setOnCompletionListener: function (func) {
        CALLBACK_FUNCS.COMPLETION = func;
    },
    setOnErrorListener: function (func) {
        CALLBACK_FUNCS.ERROR = func;
    },
    onInfo: function () {
        try {
            if (null != CALLBACK_FUNCS.INFO) {
                CALLBACK_FUNCS.INFO.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onPrepared: function () {
        try {
            // this._cLog.screenI("CCMediaPlayer onPrepared...");
            if (null != CALLBACK_FUNCS.PREPARED) {
                CALLBACK_FUNCS.PREPARED.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onSeekComplete: function () {
        try {
            if (null != CALLBACK_FUNCS.SEEKCOMPLETE) {
                CALLBACK_FUNCS.SEEKCOMPLETE.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onCompletion: function () {
        try {
            if (null != CALLBACK_FUNCS.COMPLETION) {
                CALLBACK_FUNCS.COMPLETION.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onError: function () {
        try {
            if (null != CALLBACK_FUNCS.ERROR) {
                CALLBACK_FUNCS.ERROR.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onBufferStart: function () {
        try {
            if (null != CALLBACK_FUNCS.BUFFERSTART) {
                CALLBACK_FUNCS.BUFFERSTART.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onBufferStop: function () {
        try {
            if (null != CALLBACK_FUNCS.BUFFERSTOP) {
                CALLBACK_FUNCS.BUFFERSTOP.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onTimeOut: function () {
        try {
            if (null != CALLBACK_FUNCS.TIMEOUT) {
                CALLBACK_FUNCS.TIMEOUT.apply(this, arguments);
            }
        } catch (e) {

        }
    },

    onDestroy() {
        this.release();
    },

});
//监听,放在class中,android回调找不到
window.MediaPlayer = {
    onInfo: function () {
        try {
            if (null != CALLBACK_FUNCS.INFO) {
                CALLBACK_FUNCS.INFO.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onPrepared: function () {
        try {
            if (null != CALLBACK_FUNCS.PREPARED) {
                CALLBACK_FUNCS.PREPARED.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onSeekComplete: function () {
        try {
            if (null != CALLBACK_FUNCS.SEEKCOMPLETE) {
                CALLBACK_FUNCS.SEEKCOMPLETE.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onCompletion: function () {
        try {
            if (null != CALLBACK_FUNCS.COMPLETION) {
                CALLBACK_FUNCS.COMPLETION.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onError: function () {
        try {
            if (null != CALLBACK_FUNCS.ERROR) {
                CALLBACK_FUNCS.ERROR.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onBufferStart: function () {
        try {
            if (null != CALLBACK_FUNCS.BUFFERSTART) {
                CALLBACK_FUNCS.BUFFERSTART.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onBufferStop: function () {
        try {
            if (null != CALLBACK_FUNCS.BUFFERSTOP) {
                CALLBACK_FUNCS.BUFFERSTOP.apply(this, arguments);
            }
        } catch (e) {

        }
    },
    onTimeOut: function () {
        try {
            if (null != CALLBACK_FUNCS.TIMEOUT) {
                CALLBACK_FUNCS.TIMEOUT.apply(this, arguments);
            }
        } catch (e) {

        }
    },
}