您好我正在尝试允许多个图像播放并暂停不同的歌曲,直到现在我已经完成了选择图像时播放的歌曲但是当我暂停并通过点击另一个图像尝试恢复时,之前的文件被恢复下面所需的是代码
的Javascript
function StartOrStop(audioFile)
{
var audie = document.getElementById("myAudio");
if(!audie.src || audie.src !== audioFile)
audie.src = audioFile;
if(audie.paused == false)
{
audie.pause();
}
else
{
audie.play();
}
}
HTML
<img src="images/play.png" alt="Play Button" width="57" height="50" onclick="StartOrStop('RWY.mp3')">
<img src="images/play.png" alt="Play Button" width="57" height="50" onclick="StartOrStop('EL.mp3')">
<audio controls id="myAudio" ></audio>
有关如何解决此问题的任何建议?
答案 0 :(得分:0)
嗨,不知道为什么这不起作用。我已经重新编写了你的代码,所以试试这个
function StartOrStop(audioFile)
{
var audie = document.getElementById("myAudio");
//logs to see filenames
console.log("audio src " ,audie.src); //this will output a full url e.g with www.example.com/file.mp3
console.log("file ", audioFile); // this only outputs what you pass in e.g "file.mp3"
//with the www.example.com the if statement below will be false as www.example.com/file.mp3 is not equal to file.mp3. Either change the onclick event of the images to include the full path or edit the if statement to
// if(audie.src.indexOf(audioFile) != -1){
if( audie.src == audioFile){
audie.paused ? audie.play() : audie.pause();
}else{
audie.src = audioFile;
audie.play();
}
}
我在我的机器上本地运行它,它可以按你想要的方式运行
答案 1 :(得分:0)
试试这个,现在正在运行中
希望它能帮助你......
JAVASCRIPT代码
function Start(audioFile)
{
var audie = document.getElementById("myAudio");
console.log("audio src " ,audie.src);
console.log("file ", audioFile);
if( audie.src == audioFile){
audie.paused ? audie.play() : audie.pause();
}else{
audie.src = audioFile;
audie.play();
}
}
function Stop(audioFile)
{
var audie = document.getElementById("myAudio");
console.log("audio src " ,audie.src);
console.log("file ", audioFile);
if( audie.src == audioFile){
audie.paused ? audie.play() : audie.pause();
}else{
audie.src = audioFile;
audie.Pause();
}
}
=============================================== ========
HTML代码
<img src="images/play.png" alt="Play Button" width="57" height="50" onclick="Start('RWY.mp3')">
<br>
<img src="images/stop.png" alt="StopButton" width="57" height="50" onclick="Stop('EL.mp3')">
<br>
<audio controls id="myAudio" ></audio><br>