JQuery和SoundManger玩得不好

时间:2011-02-15 08:26:39

标签: javascript jquery internet-explorer firefox soundmanager2

我只是盯着使用JQuery和SoundManger2,我注意到在使用JQuery的某些情况下SoundManager存在问题。它还取决于是否使用Firefox(3.6.13)或IE(8.0.7600)。

我试图测试的是天气,我可以播放声音。在每个示例中,我将首先显示代码,在代码之后,我将确定IE和FireFox是成功还是失败。


<html> 
<head>
    <title></title>
    <script type="text/javascript" src="/Project/PublicWebSite/Scripts/soundmanager2.js"></script>
    <script type="text/javascript" src="/Project/PublicWebSite/Scripts/jquery-1.5.js"></script>    
    <script type="text/javascript">
        soundManager.debugMode = true;
        soundManager.defaultOptions.volume = 50
        soundManager.debugFlash = true; // enable flash debug output for this page
        soundManager.url = '/Project/PublicWebSite/Scripts/swf/soundmanager2.swf';
        soundManager.flashVersion = 8; // optional: shiny features (default = 8)
        soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
        //enable HTML5 audio support, if you're feeling adventurous. iPad/iPhone will always get this.
        //soundManager.useHTML5Audio = true;
        soundManager.onready(function () {
            soundManager.createSound('helloWorld', '/Project/PublicWebSite/Content/Sounds/Chime.mp3');
            soundManager.play('helloWorld');
        });
    </script>
</head>
<body>
</body>
</html>

IE :成功; FireFox :成功


在下面的代码中,除了我在JQuery文档加载中添加SoundManager的配置外,一切都是一样的。

<html> 
<head>
    <title></title>
    <script type="text/javascript" src="/Project/PublicWebSite/Scripts/soundmanager2.js"></script>
    <script type="text/javascript" src="/Project/PublicWebSite/Scripts/jquery-1.5.js"></script>    
    <script type="text/javascript">

        $(function () {
            soundManager.debugMode = true;
            soundManager.defaultOptions.volume = 50
            soundManager.debugFlash = true; // enable flash debug output for this page
            soundManager.url = '/Project/PublicWebSite/Scripts/swf/soundmanager2.swf';
            soundManager.flashVersion = 8; // optional: shiny features (default = 8)
            soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
            //enable HTML5 audio support, if you're feeling adventurous. iPad/iPhone will always get this.
            //soundManager.useHTML5Audio = true;
            soundManager.onready(function () {
                soundManager.createSound('helloWorld', '/Project/PublicWebSite/Content/Sounds/Chime.mp3');
                soundManager.play('helloWorld');
            });
        });
    </script>
</head>
<body>
</body>
</html>

IE :成功; FireFox :失败


更改了JQuery和SoundManger脚本引用的顺序

<html> 
<head>
    <title></title>
    <script type="text/javascript" src="/Project/PublicWebSite/Scripts/jquery-1.5.js"></script>    
    <script type="text/javascript" src="/Project/PublicWebSite/Scripts/soundmanager2.js"></script>
    <script type="text/javascript">

        $(function () {
            soundManager.debugMode = true;
            soundManager.defaultOptions.volume = 50
            soundManager.debugFlash = true; // enable flash debug output for this page
            soundManager.url = '/Project/PublicWebSite/Scripts/swf/soundmanager2.swf';
            soundManager.flashVersion = 8; // optional: shiny features (default = 8)
            soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
            //enable HTML5 audio support, if you're feeling adventurous. iPad/iPhone will always get this.
            //soundManager.useHTML5Audio = true;
            soundManager.onready(function () {
                soundManager.createSound('helloWorld', '/Project/PublicWebSite/Content/Sounds/Chime.mp3');
                soundManager.play('helloWorld');
            });
        });
    </script>
</head>
<body>
</body>
</html>

IE :失败; FireFox :成功


如果我所做的只是创建静态网页,这不会是一个问题。我正在使用图层等在asp.net MVC中创建代码,并且加载内容的顺序非常重要。这就是我偶然发现这个问题的方法。

由于我是JQuery和SoundManger的菜鸟,我很有可能做错了。如果有人对如何做得更好有任何意见,我会批评任何答案。在我想出这个问题之前,我把头撞在键盘上一段时间,然后希望这会帮助别人。


更新

当声音无法播放时,我从SoundManager2

获取以下信息

- SoundManager 2无法加载(安全/加载错误) -
soundManager.disable():关闭
soundManager:初始化失败。
soundManager:验证/Project/PublicWebSite/Scripts/swf/soundmanager2.swf是否为有效路径。
soundManager:在预期时间内没有Flash响应。可能的原因:加载soundmanager2_debug.swf可能已经失败(和/或Flash 8+不存在?),Flash被阻止或JS-Flash安全性错误。有关更多调试信息,请参阅SWF输出 soundManager:不耐烦,还在等待Flash ...
soundManager :: initMovie():等待来自Flash的ExternalInterface调用。
soundManager :: initMovie():获得EMBED元素(通过JS创建)
soundManager :: createMovie():尝试加载./soundmanager2_debug.swf
- SoundManager 2 V2.97a.20110123(AS2 / Flash 8),正常轮询 -

我在Firebug和Fiddler中注意到,当我收到此错误时,SoundManger试图找到 soundmanager2_debug.swf @ / project / PublicWebSite / static /。问题是我的swf文件不在那里。这是我的HTML文件所在的位置。


更新

西蒙,这指向了正确的方向。我没有像http://www.schillmania.com/projects/soundmanager2/doc/getstarted/#lazy-loading中提到的那样更改soundmanger2.js文件中的任何内容 我只是删除了对SoundManger脚本的引用,并使用JQuery ajax调用动态加载了脚本。

<html> 
<head>
    <title></title>
    <script type="text/javascript" src="/Project/PublicWebSite/Scripts/jquery-1.5.js"></script>    

    <script type="text/javascript">

        $(function () {
            $.ajax({
                url: '/Project/PublicWebSite/Scripts/soundmanager2.js',
                dataType: 'script',
                success: 
                {
                    soundManager.debugMode = true;
                    soundManager.defaultOptions.volume = 50
                    soundManager.debugFlash = true; // enable flash debug output for this page
                    soundManager.url = '/Project/PublicWebSite/Scripts/swf/soundmanager2.swf';
                    soundManager.flashVersion = 8; // optional: shiny features (default = 8)
                    soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
                    //enable HTML5 audio support, if you're feeling adventurous. iPad/iPhone will always get this.
                    //soundManager.useHTML5Audio = true;
                    soundManager.onready(function () {
                        soundManager.createSound('helloWorld', '/Project/PublicWebSite/Content/Sounds/Chime.mp3');
                        soundManager.play('helloWorld');
                    });
                }

            });

        });
    </script>
</head>
<body>
</body>
</html>

IE :成功; FireFox :成功

BarDev

2 个答案:

答案 0 :(得分:3)

这里的问题不是特别使用jquery,而是将Soundmanager2的配置绑定到DOM-ready事件。 Soundmanager2本身也将其加载例程绑定到此事件,因此如果您的配置代码在此事件发生之前未执行,则可能已经太晚了,具体取决于浏览器调用这些事件处理程序的订单。

我不是SM2专家(从未使用它),但我偶然发现了管理器的“延迟加载”功能,这可能会有所帮助,因为它应该允许您推迟Soundmanager2的加载过程并显式加载在调用配置代码之后: http://www.schillmania.com/projects/soundmanager2/doc/getstarted/#lazy-loading

答案 1 :(得分:0)

我最近发布了一个利用SoundManager2的音频播放器框架,并将w / jQuery集成为$ .fn。 - 非常简单易用和/或自定义..类似于前端设计中的jPlayer,但具有额外的播放功能(例如RTMP流媒体,完整的HTML5支持),示例,测试等:

https://github.com/APMG/APMPlayer