1

嗨,我正在尝试允许多个图像播放和暂停不同的歌曲,直到现在我已经在选择图像时实现了歌曲的播放,但是当我暂停并尝试通过单击另一张图像恢复时,前一个文件被恢复而不是所需的文件下面是代码

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>

关于如何解决此问题的任何建议?

4

2 回答 2

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();
}

}

我已经在我的机器上本地运行了它,它可以按照你想要的方式工作

于 2013-03-23T17:53:39.970 回答
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>
于 2013-12-07T14:44:06.903 回答