我最近開始學習網絡開發,目前我正在研究一個網頁,該網頁可以讀取文件夾中的多個音頻文件,並讓用戶撰寫關於他/她聽到的內容的抄本。我前幾天開始創建,我通過php和play(通過用戶實際播放)獲得了一個文件列表,通過對數組進行整理(清楚的是,每次加載頁面時都會有一個隨機文件準備好被播放)。這似乎工作正常。
我也想添加到按鈕,讓用戶聽下一個/上一個文件,但無論何時何地我寫next($array)
線它不工作,更糟糕的是,shuffle
函數也停止工作。我不知道是不是因爲我在標籤中使用php,但是我的內心有些東西告訴我。
我不知道我是否足夠清楚,但我會很感激任何幫助。因此,這裏是我的代碼:
<html>
<head>
</head>
<body>
<p align="center">
<button><-</button>
<audio src="./Sound data/AAA2028C4_0.wav" type="audio/wav" align="center" id="RandomAudio" controls="controls">
</audio>
<button name="nextButton" onClick="next()">-></button>
</p>
<script language="JavaScript" type="text/javascript">
<?php
//Scan the sound data folder for files
$dir = './Sound data';
$files = scandir($dir);
$random_file = shuffle($files);
?>;
function load(){
var test = "./Sound data/<?php
//Select random element from files array
echo $files[$random_file];
?>";
//Play a random file
document.getElementById('RandomAudio').src=test;
}
function next(){
var next = "./Sound data/<?php
$next_file = next($random_file);
echo $files[$next_file];
?>";
//Play the next file
document.getElementById('RandomAudio').src=next;
}
window.onload = load;
</script>
<form method="post" action="testecho.php" align="center">
<strong><label>Transcript : </label></strong>
<br>
<textarea name ="transcript" rows="2" cols="40"> </textarea>
<input type="submit" />
</form>
</body>
</html>
查看網站的生成代碼。它看起來正確嗎?另外:['next'](http://php.net/next)已經返回下一個值。所以你可以馬上使用它的返回值。 – hakre