0

我想通过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>

提前致谢

4

1 回答 1

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-function),因为您在 getFlashMovieObject-Function 中返回它:

 function playMyVideoInFlash(){
    var flashMovie = getFlashMovieObject("FLVplayer");
    if(typeof flashMovie === 'object' && typeof flashMovie.playMyVideo === 'function') {
      flashMovie.playMyVideo();
    }
}
于 2012-11-26T17:07:07.247 回答