Javascript 在页面上的NiftyPlayer实例之间切换-在IE上隐藏时不会停止播放

Javascript 在页面上的NiftyPlayer实例之间切换-在IE上隐藏时不会停止播放,javascript,jquery,Javascript,Jquery,我有一个链接到MP3的页面,当点击链接时,我使用javascript在链接下显示一个小的Flash播放器层。当点击不同的链接时,旧玩家被隐藏,新玩家被显示 在Firefox中显示元素时,播放器自动启动,隐藏元素时自动停止 在IE中,它只能自动启动,不能自动停止。这就是我想要解决的问题 这是一个带有链接和播放器的HTML示例 <a href="Beat The Radar - Misunderstood What You Said.mp3" onclick="toggle_visibilit

我有一个链接到MP3的页面,当点击链接时,我使用javascript在链接下显示一个小的Flash播放器层。当点击不同的链接时,旧玩家被隐藏,新玩家被显示

在Firefox中显示元素时,播放器自动启动,隐藏元素时自动停止

在IE中,它只能自动启动,不能自动停止。这就是我想要解决的问题

这是一个带有链接和播放器的HTML示例

<a href="Beat The Radar - Misunderstood What You Said.mp3" onclick="toggle_visibility('player662431');return false;" class="mp3caption">Misunderstood What You Said</a>

<div id="player662431" class="playerhide"><embed src="http://www.example.com/shop/flash/player.swf?file=/mp3/Beat The Radar - Misunderstood What You Said.mp3&as=1" quality="high" bgcolor="#000000" width="161" height="13" name="niftyPlayer662431" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
我认为我需要做的是用javascript手动启动播放器,而不是在URL字符串中使用autostart as=1函数

NiftyPlayer附带了一些javascript来支持这一点

niftyplayer('niftyPlayer1').play()
还有一种停止方法

我需要一些javascript方面的帮助-我如何将这个调用添加到我的toggle_visibility函数中?它在播放器名称中添加了与正在显示的div ID相同的唯一ID号,但我不知道如何从一件事中提取这个ID号,然后将其放入另一件事中

我也希望能够做到

niftyplayer('niftyPlayer1').stop()
停止先前运行的播放机的音频。是否可以将当前ID号码存储在某个位置,并在需要时回拨

谢谢你的帮助,我是一个PHP程序员,需要一些Javascript支持——我知道我想要实现什么,只是不知道要实现它的命令


谢谢

如果您为每个niftyplayer对象指定了一个类名f.x..players,那么您就可以在每个播放器中循环,如下所示:

function toggle_visibility(id) {

    $(".players").each(function(){

        playerId = $(this).attr('id');

        if(niftyplayer(playerId).getState() == 'playing') {

            //Stop the currently playing player
            niftyplayer(playerId).stop();

            //Hide the div that was playing
            $("#" + playerId).hide();

        }

    });

    //Start the new player
    niftyplayer(id).play();
    $("#" + id).show();        

}
这实际上是在网站上的所有玩家之间循环。它检查每个玩家的状态是否等于播放,如果等于,则停止播放并隐藏div标记。然后它启动新播放器并显示div标签


我想这就行了。试试看。

当我在使用Internet Explorer时注意到一个非常严重的bug/“功能”后,我有了一个更好的解决方案

我注意到,在IE中,当我有很多隐藏的漂亮播放器时,页面加载需要很长时间,我使用Fiddler仔细查看,发现NiftyPlayer的每个实例都是完整预加载MP3,而不是像Firefox和Chrome等那样按需加载

这意味着,一个包含100个项目的页面,每个项目最多有4个MP3,有时需要几分钟才能加载,并且会产生明显的数据传输影响

我的解决方案比较简单,但可能比Indyber的更笨重,就是使用

function toggle_visibility(id,mp3location) {
    // hide all players
    $(".playerarea").html('');

    // show clicked player
    $('#' + id).html('<embed src=\"http://www.xxx.com/shop/flash/player.swf?file=http://www.xxx.com/mp3/' + decodeURIComponent(mp3location) + '.mp3&as=1\" quality=high bgcolor=#000000 WMODE=transparent width=\"161\" height=\"13\"  align=\"\" type=\"application/x-shockwave-flash\" swLiveConnect=\"true\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" class=\"playerNew\">');


}

这与IE配合得很好,也解决了无法阻止玩家在IE中玩的问题

谢谢,这正是我需要的!不幸的是,Firebug说niftyplayerplayerId在ifniftyplayerplayerId.getState=='playing'行中未定义{这可能是因为没有正确调用niftyplayer。我如何调试它?谢谢!Ashley调试它的第一件事是检查变量playerId包含什么?它应该是您当前正在循环的播放器的id,例如niftyPlayer1、niftyPlayer2等。您只需确保每个播放器都是div标记的子项与您嵌入的flash对象具有相同的id/名称,并且该div标记具有一个类players。然后它可以在这些播放器中循环,获取它们的id,并检查它们是否正在播放。如果在调试它时playerId未定义console.debugplayerId,则检查$this的输出。它应该是一个div标记。您好,谢谢按照您的建议,我们现在正在避免playerId未定义消息。不幸的是,它遇到的下一个javascript错误是在niftyplayer.js的这一部分,这是我尝试单击指向javascript:niftyplayer'niftyPlayer2'的链接时出现的。play:movieIsLoaded:function theMovie{theMovie.PercentLoaded如果typeofmoie!=未定义返回theMovie.PercentLoaded==100,则不是函数;否则返回false;错误为'theMovie.PercentLoaded'不是函数我不熟悉PercentLoaded-它似乎是某种flash函数,它会导致多个玩家出现问题吗?哦,可怕的PercentLoadedaded错误。我已经有过很多次了。通常这意味着flash电影找不到它应该播放的文件。你确定.mp3文件的路径正确吗?而且你不必担心是否可以每页使用多个NIFTyplayer,因为我目前正在一个至少有4个播放器的网站上工作它可以跨所有浏览器工作。因此,请确保指向.mp3文件的所有路径都正确,然后重试。
function toggle_visibility(id,mp3location) {
    // hide all players
    $(".playerarea").html('');

    // show clicked player
    $('#' + id).html('<embed src=\"http://www.xxx.com/shop/flash/player.swf?file=http://www.xxx.com/mp3/' + decodeURIComponent(mp3location) + '.mp3&as=1\" quality=high bgcolor=#000000 WMODE=transparent width=\"161\" height=\"13\"  align=\"\" type=\"application/x-shockwave-flash\" swLiveConnect=\"true\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" class=\"playerNew\">');


}