[英]Change multiple images when clicking button while affecting audio
I have searched Stack and have not come across a method that works in this situation. 我已经搜索了Stack,但没有遇到一种在这种情况下有效的方法。
Here is the final goal. 这是最终目标。 I have lets say 10 songs on a page and when I click a button with image it plays the song, and when i click the next songs play button the first song stops and the second one plays. 我可以说一页上有10首歌曲,当我单击带有图像的按钮时,它将播放歌曲,而当我单击下一首歌曲播放按钮时,第一首歌曲停止播放,第二首歌曲播放。 And if I click the current songs play button it pauses as well. 如果我单击当前的歌曲播放按钮,它也会暂停。
What I am having trouble with is getting the play button imagae to change to a pause image on click and have it go back to play image when i either click the pause button or click the play button of another song. 我遇到的麻烦是,当我单击“暂停”按钮或单击另一首歌曲的“播放”按钮时,使“播放”按钮的图像变为单击时变为暂停图像,并使其返回播放图像。
Below is my code how I have it so far... In this example I show only two songs, but on the actually site there will be anywhere from 10 -15 songs 下面是我到目前为止的代码...在此示例中,我仅显示两首歌曲,但是在实际站点上,会有10 -15首歌曲
I commented out the thisimage.attr in the script as it is breaking. 我在脚本中注释了thisimage.attr,因为它已损坏。 I also tried thisimage.src but it didnt work either. 我也尝试了thisimage.src,但是它也不起作用。 With this script the audio functions exactly the way I need it to. 使用此脚本,音频可以完全按照我需要的方式运行。
HTML -- HTML-
<!-- Music Button 1 -->
<div class="col-sm-1 col-xs-2">
<button onclick="EvalSound('song1')">
<img id="song1a" src="/play.png" class="img-responsive" />
</button>
</div>
<audio id="song1">
<source src="/song1.mp3" type="audio/mpeg">
</audio>
<!----/MUSIC BUTTON 1---->
<!-- Music Button 2-->
<div class="col-sm-1 col-xs-2">
<button onclick="EvalSound('song2')">
<img id="song2a" src="/play.png" class="img-responsive" />
</button>
</div>
<audio id="song2">
<source src="/song2.mp3" type="audio/mpeg">
</audio>
<!----/MUSIC BUTTON 2---->
JavaScript -- JavaScript-
<script type="text/javascript">
var currentPlayer;
function EvalSound(sound) {
var thissound = document.getElementById(sound);
var animage = sound +'a';
var thisimage = document.getElementById(animage);
if(currentPlayer && currentPlayer != thissound) {
currentPlayer.pause();
}`enter code here`
if (thissound.paused) {
thissound.play();
thisimage.attr("src", "/Pause.png");
currentPlayer = thissound;
}
else{
thissound.pause();
thisimage.attr("src", "/play.png");
thissound.currentTime = 0;
currentPlayer = thissound;
}
currentPlayer = thissound;
}
</script>
This is not the best practice approach, you should use only one audio
tag for global player. 这不是最佳做法,对于全局播放器,您应该只使用一个audio
标签。 But for your solution , i think this changes might work for you: 但是对于您的解决方案,我认为此更改可能对您有用:
var currentPlayer; var currentPlayer;
var currentImage; var currentImage;
function EvalSound(sound) { 函数EvalSound(sound){
var thissound = document.getElementById(sound);
var animage = sound +'a';
var thisimage = document.getElementById(animage);
if(currentPlayer && currentImage &&
currentImage != thisimage
&& currentPlayer != thissound) {
currentPlayer.pause();
currentPlayer.currentTime = 0; currentImage.attr("src", "/play.png");
}
if (thissound.paused) {
thissound.play();
thisimage.attr("src", "/Pause.png");
currentPlayer = thissound;
}
else{
thissound.pause();
thisimage.attr("src", "/play.png");
thissound.currentTime = 0;
currentPlayer = thissound;
}
currentPlayer = thissound;
currentImage = thisimage;
} }
I was able to get this to work based off of Joey Etamity's response: 根据Joey Etamity的回复,我能够使它工作:
<script type="text/javascript">
var currentPlayer;
var currentImage;
function EvalSound(sound) {
var thissound = document.getElementById(sound);
var animage = sound +'a';
var thisimage = document.getElementById(animage);
if(currentPlayer && currentPlayer != thissound) {
currentPlayer.pause();
}
if(currentImage && currentImage != thisimage) {
currentImage.src = '/playicon.png';
}
if(thissound.play){
thisimage.src = '/Pause.png';
}
if (thissound.paused) {
thissound.play();
if(thissound.play){
thisimage.src = '/Pause.png';
}
currentPlayer = thissound;
}
else{
thissound.pause();
if(thissound.pause){
thisimage.src = '/playicon.png';
}
thissound.currentTime = 0;
currentPlayer = thissound;
}
currentPlayer = thissound;
currentImage = thisimage;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.