如何从声音的开始播放/暂停声音和声音播放?

时间:2016-11-10 09:28:42

标签: javascript audio

我希望用户能够点击al链接,如果用户再次点击它停止的链接,声音将开始播放,如果它再次播放,但声音将从开始时开始,并从它之前停止了。 我的代码现在只是一个简单的切换声音:

var partOne = document.getElementById("part1");
function PlayPart1() {
  return partOne.paused ? partOne.play() : partOne.pause();
};
  <p>
<a   class="textAudio" onClick="PlayPart1()">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit 
</a>
    </p>
	<audio id="part1" src="http://www.sousound.com/music/healing/healing_01.mp3" preload="auto"></audio>

1 个答案:

答案 0 :(得分:3)

您可以使用currentTime。像这样使当前时间为0 -

&#13;
&#13;
var partOne = document.getElementById("part1");
function PlayPart1() {
partOne.currentTime = 0
  return partOne.paused ? partOne.play() : partOne.pause();
};
&#13;
  <p>
<a   class="textAudio" onClick="PlayPart1()">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit 
</a>
    </p>
	<audio id="part1" src="http://www.sousound.com/music/healing/healing_01.mp3" preload="auto"></audio>
&#13;
&#13;
&#13;