4

I am using soundcloud custom player to create a player which can play all the songs on my site. This works pretty good when i just place static url of any track or a post. But what i want is to add the song to the list dynamically.

I have done lots of research but couldn't get it to work. What i want is there would be multiple players through out the site. kind of(http://soundcloud.com) and there would a main player on the top(like soundcloud) which would play around 100 latest songs on site and there would be a smaller player clicking on which will just append that song to the list and start playing that song.

I am not sure if this is the right process. If any one can just give me any hint how i can proceed further then it would be great.

4

2 回答 2

6

在 Wiki 中进行了大量研究、研究 中的代码sc-player.js并进行了一些测试之后,我想我已经找到了解决您问题的方法。尝试以下操作:

首先,将此代码添加到sc-player.js文件中,就在注释之前// the default Auto-Initialization(这一行就像文件末尾之前的 8 行)

$.scPlayer.loadTrackInfoAndPlay=function($elem,track){
        var playerObj=players[$elem.data('sc-player').id];
        playerObj.tracks.push(track);
        var index =playerObj.tracks.length-1;
        var $list=$(playerObj.node).find('.sc-trackslist');
        var $artworks=$(playerObj.node).find(".sc-artwork-list");
        // add to playlist
        var $li=$('<li><a href="' + track.permalink_url +'">' + track.title + '</a><span class="sc-track-duration">' + timecode(track.duration) + '</span></li>')
            .data('sc-track', {id:index})
            .appendTo($list);
        // add to artwork list
        $('<li></li>')
            .append(artworkImage(track, true))
            .appendTo($artworks)
            .data('sc-track', track);
        $li.click();
    }

上面的函数负责将新曲目添加到播放列表并开始播放曲目。

现在,假设你有这个播放器的 html 代码

<h2>Top Player</h2>
<div id="topPlayer">
    <div class="sc-player">
        <a href="http://soundcloud.com/matas/matas-petrikas-live-at-gravity-club-30-05-2008">My live track</a>
        <a href="http://soundcloud.com/matas/on-the-bridge">On the bridge</a>
    </div>
</div>
<h2>Small Player</h2>
<div id="smallPlayer">
    <a href="http://soundcloud.com/forss/sets/soulhack" class="sc-player">Soulhack</a>
</div>

确保将您sc-player的 s 包装在 a 中div,最好使用 an id,以便您稍后可以在 Javascript 中引用播放器:

最后,您可以使用此 Javscript 代码来检测何时单击曲目并在smallPlayer其中添加和播放该曲目topPlayer

$(function(){
    var $topPlayer=$("#topPlayer .sc-player");//Top player
    var $smallPlayer=$("#smallPlayer .sc-player");//Small Player
    $smallPlayer.on("onPlayerInit.scPlayer",function(evt){//When player is ready
        $smallPlayer.on("onPlayerTrackSwitch.scPlayer",function(evt,track){//Listen to track switch in smallPlayer
            setTimeout(function(){//Wait a bit, to avoid playing problems between both players
                $.scPlayer.loadTrackInfoAndPlay($topPlayer, track);
            },250);
        });
    });
});

就是这样!

编辑

如果您只有要添加的歌曲的 url,您可以执行以下操作:

首先,将另一个函数添加到sc-player.js文件中,就在前一个loadTrackInfoAndPlay函数之后:

$.scPlayer.loadTrackUrlAndPlay=function($elem,url){
    var apiUrl = scApiUrl(url, apiKey);
    $.getJSON(apiUrl, function(data) {
        if(data.duration){
          data.permalink_url = url;
          $.scPlayer.loadTrackInfoAndPlay($elem,data);//Call the previous function
        }
    });
}

现在,假设您有这些歌曲网址的代码:

<p>
    The links <br />
    <a class="songUrl" href="https://soundcloud.com/feintdnb/coldplay-fix-you-feint-bootleg">Coldplay-Fix you</a><br />
    <a class="songUrl" href="https://soundcloud.com/nct-1/nct-you-preview-out-soon-for">NCT - You</a><br />
    <a class="songUrl" href="https://soundcloud.com/tufftouch/devil-eyes-konflix-feat-leanne">Konflix - Devil Eyes</a>
</p>

然后,您可以使用此代码将歌曲添加到播放列表并开始播放(如果已经存在,则直接播放)

$(".songUrl").click(function(event){
    event.preventDefault();
    var url=$(this).attr("href");//Url of the song
    //If the song is in the tracks list, this line finds it
    var $theSong=$topPlayer.find(".sc-trackslist a[href='"+url+"']");
    if($theSong.length==0){//
        $.scPlayer.loadTrackUrlAndPlay($topPlayer,url );
    }else{
        $theSong.parent().click();//Fire click = play the song
    }
});

这就是我们所需要的。

于 2013-09-07T19:28:11.633 回答
0

我会尽力为您提供尽可能多的信息,但由于无处可去,我不确定它是否会有所帮助。从wiki开始很重要。这是我可以从那里提取的:

动态轨道加载

播放器可以即时创建,因此可以添加任何 url。为此,可以使用以下方法:

直接附加到元素:

    $('div.your-container-class').scPlayer({
        links: [{url: "http://soundcloud.com/matas/hobnotropic", title: "http://soundcloud.com/matas/hobnotropic"}]
    });

创建对象并附加到您想要的位置:

    var $myPlayer  = $.scPlayer({
        links: [{url: "http://soundcloud.com/matas/hobnotropic", title: "http://soundcloud.com/matas/hobnotropic"}]
    });
    $myPlayer.appendTo($('#somewhere-deep-in-the-dom'));

我刚刚发现的一个小例子:

    $(function() {
        $('a.sc-remote-play1').on('click', function(event) {
            $('.sc-player').scPlayer({
                links: [{url: "http://soundcloud.com/matas/hobnotropie", title:   "Hobnotropic"}],
            });
        });
    }); 

我相信应该解决动态轨道加载。

初始化 scPlayer

重要的是要考虑到 scPlayer 必须自行加载,因此如果您直接使用 jQuery 更改 url,它将无法工作。为了初始化 DOM 加载后发生的 scPlayer,您可以使用

$('a.your-link-class').scPlayer();

或者

$('div.your-container-class').scPlayer();

正如我所说,默认加载发生在使用以下方法加载 DOM 时

$('a.sc-player, div.sc-player').scPlayer();

可以按如下方式更改此默认行为:

$.scPlayer.defaults.onDomReady = function(){
  // Default behaviour wanted. Per default we have:
  $('a.your-link-class').scPlayer();
};

这就是我现在所做的。尝试让 scPlayer 工作(动态加载歌曲),然后我们可以跟进数据加载和所有这些内容。

于 2013-09-07T15:15:20.060 回答