2013-04-12 53 views
0

我一直在爲我的網站創建一個彈出式播放器,用戶可以播放其他用戶已上傳到該網站的音樂播放列表。不過目前我不得不手動點擊每首曲目來播放它,我想知道是否有人設法創建了一個<cfmediaplayer>,它會自動播放播放列表中的下一首歌曲。它知道當前播放哪首歌曲的方式是在發送用戶點擊的曲目名稱的url變量中定義的,然後調用數據庫以查看它是否可以找到匹配,然後使用源輸出跟蹤我存儲。生病後的代碼給我在做什麼更明確的看法:<cfmediaplayer>如何自動播放下一首曲目

<cfquery datasource="#mydatabase#" name="getsongs"> 
    Select * 
    From artists, songs 
    Where artist_id = song_artistid 
</cfquery> 

<cfif IsDefined("URL.song")> 
    <cfquery datasource="#mydatabase#" name="playsong"> 
     Select * 
     From songs 
     Where artist_id = song_artistid 
     And song_name = '#URL.song#' 
    </cfquery> 
</cfif> 

<div style="margin: 0 auto; width: 800px;> 
    <div style="width: 300px; float: left; overflow: scroll;"> 
     <cfoutput query="getartists"> 
      <a href="popup_player.cfm?song=#song_name#>#artist_name# - #song_name#</a> 
     </cfoutput> 
    </div> 

    <cfif IsDefined("URL.song")> 
     <div style="width: 500px; float: right;"> 
      <cfoutput> 
       <cfmediaplayer name="song" source="artists/songs/#playsong.song_location#" autoplay="yes" height="500" width="500"></cfmediaplayer> 
      </cfoutput> 
     </div> 
    </cfif> 
</div> 

正如你所看到的,我有一個滾動的div其中列出了所有的歌曲將在用戶的播放列表。通過點擊歌曲,它會更新媒體播放器播放的曲目。我想知道是否還有,當歌曲結束時可以自動點擊下一個鏈接,也許有一種方法,如果一個變量已經設置爲重複播放列表,當它到達最後一個音軌時,它會再次返回到第一個音軌。

回答

1

請參閱CFMEDIAPLAYER參考指南(http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSE66DB0CD-E16D-49e7-AAEE-F51F9580554E.html)。它支持在軌道結束時運行JavaScript。您可以使用它自動調用下一個曲目。

+0

乾杯!唯一的問題是,我如何編寫JavaScript來檢查播放列表中下一首曲目的內容?當用戶創建一個播放列表時,它會爲它們創建一個唯一的XML,所以想知道是否可以爲用戶設置的軌道創建播放列表編號,以便在解析和選擇下一個軌道時更輕鬆。 – Banny

+0

如果你知道跟蹤服務器端,你可以輸出它們作爲你的代碼可以使用的JS變量。請參閱toScript()。這是另一篇博文,可能有幫助:http://www.raymondcamden.com/index.cfm/2012/8/27/Example-of-M3U-Support-for-CFMEDIAPLAYER –

+0

是的,我可以通過他們讀取服務器端而不是必須物理地聲明它們在客戶端,使用toScript()函數我應該能夠輕鬆地傳遞當前行的值,從而爲我提供正確的鏈接。 – Banny

0

當歌曲結束時,您可以運行JavaScript。 添加到您當前密碼:

<cfmediaplayer onComplete=" YOUR_PLAY_NEXT_FUNCTION" name="song" 
source="artists/songs/#playsong.song_location#" autoplay="yes" 
height="500" width="500"></cfmediaplayer> 

添加了來自評論

我haven't試過,但做這項工作嗎?

var i = 0; // This should be the current song_no 
$("#button").on('click', function(){ 
    // Adds +1 to the current song number 
    i++ 
    // Create a variable using the standard url + the added song-number 
    var curr_song = "artists/songs/#playsong.song_location#"+i; 
    // Changes the attribute to the new song-url 
    $('cfmediaplayer').attr("name",curr_song); 
}); 
+0

我唯一的問題是JavaScript;我怎麼能讓它知道下一個要播放哪條曲目?我編輯的一件事是我已經將一個playlistNo變量添加到值爲當前行的URL中,以便它知道在查詢中跳轉到哪裏。我所需要的只是當它結束時它知道去鏈接 Banny

+0

@Leeb我還沒有嘗試過,但這樣做對你有用嗎? 'var i = 0; // This should be the current song_no $(「#button」)。on('click',function( ){ i ++ //將+1加到當前歌曲編號 var curr_song =「artists/songs /#playsong.song_location#」+ i; //使用標準網址+添加的歌曲編號創建一個變量 $('cfmediaplayer')。attr(「name」,curr_song); //將屬性更改爲新歌曲url });' – MDDY

+0

如果JavaScript顯示播放列表所在的當前行,這將是一個好的解決方案。不幸的是,它不是,每首歌都有自己的行,這取決於它在查詢中的位置,這就是我想要通過編輯當前行並自動移動到列表中的下一行來控制軌道導航。 – Banny

0

好吧,我從來沒有使用cfmediaplayer之前,但這個僞代碼應該讓你到你需要的地方。

這些是我使用的資源。

  1. Dynamically starting and stopping a video using JavaScript and the CFMEDIAPLAYER tag in ColdFusion
  2. ColdFusion.Mediaplayer.setSource
  3. cfmediaplayer

<cfquery ...> 
Select 
    songUrl 
from 
    songs 
</cfquery> 
<!--- build your coldFusion array of song URLs ---> 
<cfset urlList = valueList(query.songURL)> 
<cfset urlArray = listToArray(urlList)> 

<script> 
    <cfoutput> 
    #toScript(urlArray)# 
    var songIndex = #query.currentRow-1#; <!--- or url.currentRow, whatever you're using ---> 
    </cfoutput> 
    startNext(){ 
     songIndex++; 
     if(songIndex <= urlArray.length){     
      ColdFusion.Mediaplayer.setSource('mediaPlayerName', urlArray[songIndex]); 
      ColdFusion.Mediaplayer.startPlay('mediaPlayerName'); 
     } 
    } 
</script> 

相關問題