1

我正在尝试使用 JPlayer(http://jplayer.org)制作一个网络小部件,并按照此处所述的程序进行操作:http: //alexmarandon.com/articles/web_widget_jquery/

这是我的 widget.js 文件,它将从外部网站调用以加载 Web 小部件:

(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");

    var script_tag1 = document.createElement('script');
    script_tag1.setAttribute("type","text/javascript");
    script_tag1.setAttribute("src",
        "http://abcradiobd.fm/Scripts/jquery.jplayer.min.js");

    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    //load another jquery file


    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    jQuery(document).ready(function($) { 
        /******* Load CSS *******/
        var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "http://abcradiobd.fm/style/jplayer.blue.monday.css" 
        });
        css_link.appendTo('head');

         $("#jquery_jplayer_1").jPlayer({
        ready: function () {
            $(this).jPlayer("setMedia", {
                mp3:"http://live.abcradiobd.fm:8282/;stream/1"
            }).jPlayer("play");
        },
        swfPath: "js",
        solution: "flash, html",
        supplied: "mp3",
        wmode: "window"


    });
$("#jquery_jplayer_1").bind($.jPlayer.event.play,function(event){$(".jp-status").html("<marquee behavior='scroll' direction='left' scrollamount='5'>abc radio fm 89.2</marquee>");
    });

    $("#jquery_jplayer_1").bind($.jPlayer.event.pause,function(event){$(".jp-status").text(" paused..");
    });         



        var html_data = '<div id="jquery_jplayer_1" class="jp-jplayer"></div>'

        +'<div id="jp_container_1" class="jp-audio">'
        +   '<div class="jp-type-single">'
        +       '<div class="jp-gui jp-interface">'
        +           '<ul class="jp-controls">'
        +               '<li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>'
        +               '<li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>'
        +               '<li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>'
        +               '<li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>'
        +               '<li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>'
        +               '<li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>'
        +           '</ul>'
        +           '<div class="jp-progress">'
        +               '<div class="jp-current-time"></div>'   
        +               '<div class="jp-status"></div>'


        +           '</div>'
        +           '<div class="jp-volume-bar">'
        +               '<div class="jp-volume-bar-value"></div>'
        +           '</div>'

        +       '</div>'

        +       '<div class="jp-no-solution">'
        +           '<span>Update Required</span>'
        +           'To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>'
        +       '</div>'
        +   '</div>'
        +'</div>';     

        /******* Load HTML *******/
        //var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?";
       // $.getJSON(jsonp_url, function(data) {
          $('#widget').html(html_data);
       // });
    });
}

})(); // We call our anonymous function immediately

当我在外部网站中加载 js 文件时,我使用 firebug 收到此错误:

TypeError: $(...).jPlayer is not a function

wmode: "window"    widget.js (line 67)

谁能帮我在这里找到问题?提前致谢。

更新代码:

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");

    var script_tag1 = document.createElement('script');
    script_tag1.setAttribute("type","text/javascript");
    script_tag1.setAttribute("src",
        "http://abcradiobd.fm/Scripts/jquery.jplayer.min.js");

    if (script_tag.readyState && script_tag1.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag1);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}
4

1 回答 1

1

最终更新

HTML

<div id="abc_widget"></div>
<script type="text/javascript" src="test.js"></script>

JS

 (function() {
      // Localize jQuery variable
      var jQuery;

      /******** Load jQuery if not present *********/
      if(window.jQuery===undefined||window.jQuery.fn.jquery!=='1.6.2') {
           var jQueryTag=document.createElement('script');
           jQueryTag.setAttribute("type","text/javascript");
           jQueryTag.setAttribute("src",
           "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");

           if(jQueryTag.readyState) {
                jQueryTag.onreadystatechange=function() { // For old versions of IE
                     if(this.readyState=='complete'||this.readyState=='loaded') {
                          addJplayer();
                     }
                };
           } else {
                jQueryTag.onload=addJplayer;
           }
           // Try to find the head, otherwise default to the documentElement
           (document.getElementsByTagName("head")[0]||document.documentElement).appendChild(jQueryTag);
      } else {
           // The jQuery version on the window is the one we want to use
           jQuery=window.jQuery;
           addJplayer();
      }

      /******** adding jplayer function ********/
      function addJplayer() {
           var jPlayerTag=document.createElement('script');
           jPlayerTag.setAttribute("type","text/javascript");
           jPlayerTag.setAttribute("src",
       "http://abcradiobd.fm/Scripts/jquery.jplayer.min.js");

           if(jPlayerTag.readyState) {
                jPlayerTag.onreadystatechange=function() { // For old versions of IE
                     if(this.readyState=='complete'||this.readyState=='loaded') {
                          widgetMain(); //call main now
                     }
                };
           } else {
                jPlayerTag.onload=widgetMain; //call main now
           }
           // Try to find the head, otherwise default to the documentElement
           (document.getElementsByTagName("head")[0]||document.documentElement).appendChild(jPlayerTag);
      }

      /******** Our main function ********/
      function widgetMain() {
           // Restore $ and window.jQuery to their previous values and store the
           // new jQuery in our local jQuery variable
           jQuery=window.jQuery.noConflict(true);

           jQuery(document).ready(function($) {

                /******* Load CSS *******/
                var css_link=$("<link>",{
                     rel: "stylesheet",
                     type: "text/css",
                     href: "http://abcradiobd.fm/style/jplayer.blue.monday.css"
                });
                css_link.appendTo('head');

                var html_data='<div id="jquery_jplayer_1" class="jp-jplayer"></div>'

                +'<div id="jp_container_1" class="jp-audio">'
                +'<div class="jp-type-single">'
                +'<div class="jp-gui jp-interface">'
                +'<ul class="jp-controls">'
                +'<li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>'
                +'<li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>'
                +'<li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>'
                +'<li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>'
                +'<li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>'
                +'<li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>'
                +'</ul>'
                +'<div class="jp-progress">'
                +'<div class="jp-current-time"></div>'
                +'<div class="jp-status"></div>'


                +'</div>'
                +'<div class="jp-volume-bar">'
                +'<div class="jp-volume-bar-value"></div>'
                +'</div>'

                +'</div>'

                +'<div class="jp-no-solution">'
                +'<span>Update Required</span>'
                +'To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>'
                +'</div>'
                +'</div>'
                +'</div>';

                /******* Load HTML *******/
                $('#abc_widget').html(html_data);

                var stream={
                     title: "ABC",
                     mp3: "http://live.abcradiobd.fm:8282/;stream/1"
                };
                var jPready=false;

                $("#jquery_jplayer_1").jPlayer({
                     ready: function(event) {
                          jPready=true;
                          $(this).jPlayer("setMedia",stream).jPlayer("play");
                     },
                     pause: function() {
                          $(this).jPlayer("clearMedia");
                     },
                     error: function(event) {
                          if(jPready&&event.jPlayer.error.type===$.jPlayer.error.URL_NOT_SET) {
                               // Setup the media stream again and play it.
                               $(this).jPlayer("setMedia",stream).jPlayer("play");
                          }
                     },
                     swfPath: "js",
                     supplied: "mp3",
                     preload: "none",
                     wmode: "window",
                     keyEnabled: true
                });

                $("#jquery_jplayer_1").bind($.jPlayer.event.play,function(event) {
                     $(".jp-status").html("<marquee behavior='scroll' direction='left' scrollamount='5'>abc radio fm 89.2</marquee>");
                });
                $("#jquery_jplayer_1").bind($.jPlayer.event.pause,function(event) {
                     $(".jp-status").text(" paused..");
                });
           });
      }

 })(); // We call our anonymous function immediately

这里的测试用例:http: //portableideas.net/projects/stackoverflow/16357493/test.html (我没有上传.swf,所以用支持mp3的浏览器进行测试,比如我测试的Chrome 26) .

于 2013-05-03T12:53:49.187 回答