我使用php和javascript創建了一個在線音樂播放器,我無法弄清楚處理問題的最佳方式。我創建了一個包含歌曲名稱,藝術家,專輯等的MySQL數據庫。需要從播放列表中將歌曲名稱導入「正在播放」橫幅w/php&javascript
我從一個XML文件創建了一個播放列表,這個XML文件是從頁面上出現的MySQL數據庫生成的,而且我唯一的功能當我點擊播放列表中的歌曲時,get就會使歌曲的名稱出現在「正在播放」標題中。
我想我需要某種「onClick」函數,但我無法弄清楚我需要做什麼。另外,我顯然不想刷新頁面來播放新歌。
感謝您的幫助!
我的播放器的基本代碼如下。實際播放器使用自定義界面和XML播放列表,但基本上這是它。我只需要弄清楚如何創建「正在播放」功能。謝謝!
<html>
<head>
<script type="text/javascript">
function loadPlayer() {
var audioPlayer = new Audio();
audioPlayer.controls="controls";
audioPlayer.addEventListener('ended',nextSong,false);
audioPlayer.addEventListener('error',errorFallback,true);
document.getElementById("player").appendChild(audioPlayer);
nextSong();
}
function nextSong() {
if(urls[next]!=undefined) {
var audioPlayer = document.getElementsByTagName('audio')[0];
if(audioPlayer!=undefined) {
audioPlayer.src=urls[next];
audioPlayer.load();
audioPlayer.play();
next++;
} else {
loadPlayer();
}
} else {
alert('the end!');
}
}
function errorFallback() {
nextSong();
}
function playPause() {
var audioPlayer = document.getElementsByTagName('audio')[0];
if(audioPlayer!=undefined) {
if (audioPlayer.paused) {
audioPlayer.play();
} else {
audioPlayer.pause();
}
} else {
loadPlayer();
}
}
function pickSong(num) {
next = num;
nextSong();
}
var urls = new Array();
urls[0] = '/testphp/upload/Sleep Away.mp3';
urls[1] = '/testphp/upload/Kalimba.mp3';
urls[2] = '/testphp/upload/Sleep Away.mp3';
urls[3] = '/testphp/upload/Kalimba.mp3';
var next = 0;
</script>
</head>
<body>
<div id="player"></div>
<a href="#" onclick="playPause()">Play/Pause!</a> |
<a href="#" onclick="nextSong()">Next!</a><br><br>
<a href="#" onclick="pickSong(0)">Sample 1</a><br>
<a href="#" onclick="pickSong(1)">Sample 2</a><br>
<a href="#" onclick="pickSong(2)">Missing File</a><br>
<a href="#" onclick="pickSong(3)">Sample 3</a>
</body>
</html>
您至少需要給我們一些代碼來看看。沒有辦法猜測你是如何做到這一點的。 – OptimusCrime