播放音频和的onclick重新启动 [英] Play audio and restart it onclick

查看:147
本文介绍了播放音频和的onclick重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待重新启动在HTML5音频播放器的音频文件。我已经定义了一个音频文件和播放按钮。

I'm looking to restart an audio file in a HTML5 audio player. I have defined a audio file and a play button.

<audio id="audio1" src="01.wav"></audio>
<button onClick="play()">Play</button>

当我点击播放按钮音频文件开始播放,但是当我点击该按钮再次音频文件不会停止,直到它达到不能再玩该文件的末尾。

When I click the play button the audio file starts playing, but when I click the button again the audio file doesn't stop and will not play again until it reaches the end of the file.

function play() {
    document.getElementById('audio1').play();
}

有没有可以让我重新启动音频文件时使用我点击按钮的方法的onclick ,而不是等待歌停止?

推荐答案

要只需重新启动这首歌,你会怎么做:

To just restart the song, you'd do:

function play() {
    var audio = document.getElementById('audio1');
    if (audio.paused) {
        audio.play();
    }else{
        audio.currentTime = 0
    }
}

FIDDLE

要切换它,因为在音频再次点击时停止,而当点击它从头重新开始另一次,你会做更多的东西一样:

To toggle it, as in the audio stops when clicking again, and when click another time it restarts from the beginning, you'd do something more like :

function play() {
    var audio = document.getElementById('audio1');
    if (audio.paused) {
        audio.play();
    }else{
        audio.pause();
        audio.currentTime = 0
    }
}

FIDDLE

这篇关于播放音频和的onclick重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆