未捕获的TypeError javascript与flash通信

时间:2012-11-26 17:01:44

标签: javascript html5

我想通过javascript与flash通信,以控制我的Flash播放器 这是我的代码,但我有一个错误,请你帮帮我,

    

    var flashvars = {
        flvpVideoSource: "myApp.swf",
        flvpWidth: "640",
        flvpHeight: "360"
    };
    var params = {
        menu: "true",
        allowfullscreen: "true"
    };
    var attributes = {
        id: "FLVplayer",
        name: "FLVplayer"
    };


    //functions used to find the id of the flash object
    function getFlashMovieObject(movieName){
        if(document.embeds[movieName])
        return document.embeds[movieName];
        if(window.document[movieName])
        return window.document[movieName];
        if(window[movieName])
        return window[movieName];
        if(document[movieName])
        return document[movieName];
        return null;
    }

    //function in flash to play the video
    function playMyVideoInFlash(){
        var flashMovie = getFlashMovieObject("FLVplayer");
        flashMovie.playMyVideo();
    }

    //function in flash to pause the video
    function pauseMyVideoInFlash(){
        var flashMovie = getFlashMovieObject("FLVplayer");
        flashMovie.pauseMyVideo();
    }
    </script>

这是我的错误:

 Uncaught TypeError: Cannot call method 'playMyVideo' of null index.html:37
playMyVideoInFlash index.html:37
onclick

我的按钮代码:

 <button type="button"  onclick="playMyVideoInFlash()">play</button>
    <button type="button" onclick="pauseMyVideoInFlash()">pause</button>

提前致谢

1 个答案:

答案 0 :(得分:0)

使用花括号,它应该工作(或写一行!),javascript会在每行后自动插入分号!!!

带花括号的

function getFlashMovieObject(movieName){
    if(document.embeds[movieName]) {
      return document.embeds[movieName];
    }
    if(window.document[movieName]) {
      return window.document[movieName];
    }
    if(window[movieName]) {
      return window[movieName];
    }
    if(document[movieName]) {
      return document[movieName];
    }
    return null;
}

一行:

function getFlashMovieObject(movieName){
    if(document.embeds[movieName]) return document.embeds[movieName];
    if(window.document[movieName]) return window.document[movieName];
    if(window[movieName]) return window[movieName];
    if(document[movieName]) return document[movieName];
    return null;
}

你还应该在你的函数中检查NULL(可能还有playMyVideo / stopMyVideo函数存在),只是因为你在getFlashMovieObject函数中返回它:

 function playMyVideoInFlash(){
    var flashMovie = getFlashMovieObject("FLVplayer");
    if(typeof flashMovie === 'object' && typeof flashMovie.playMyVideo === 'function') {
      flashMovie.playMyVideo();
    }
}