Javascript音频播放/停止

时间:2012-03-06 16:49:08

标签: jquery jquery-mobile

仍有问题 - 如果我让我的代码播放所选音频,按钮将不会切换。如果我包含$(document).ready(function(){,则按钮会切换,但音频将无法播放!任何人都可以保存我留下的小头发吗?这是一个用于在iOS 5上交付的HTML5 jQuery Mobile项目

感谢您提供任何/全部帮助 Ĵ

代码:

$(document).ready(function(){
var selSound = '18000'; //default to first sound.
var play = false; //default to paused.

function selectSound(id) {
    selSound = id;
}

$('#playbtn').click(function() {
     var $this = $(this);
var a = document.getElementById(selSound);   
     if($this.text() == 'Zap!') {
    $this.children().children().text('Stop');
    a.play();
} else {
    $this.children().children().text('Zap!');
     a.pause();
    alert('Stopped playing song: '+selectedMP3);



}

});

})

HTML:

<div data-role="fieldcontain">       
        <fieldset data-role="controlgroup" >
                    <li data-swatch="b" class="ui-li ui-li-divider ui-btn ui-bar-a ui-corner-top" data-role="list-divider" role="" data-form="ui-bar-b">Select a frequency</li>

    <input type="radio" name="mp3_option" id="selectSound('16000')" onclick="selectSound('16000')"><label for="selectSound('16000')">16Hz</label>

    <input type="radio" name="mp3_option" id="selectSound('18000')" onclick="selectSound('18000')"><label for="selectSound('18000')">18Hz</label>

    <input type="radio" name="mp3_option" id="selectSound('20000')"    onclick="selectSound('20000')"><label for="selectSound('20000')">20Hz</label>
</fieldset>
<div style="text-align:center"><a href="#" data-role="button" data-inline="true"   id="playbtn">Zap!</a></div></div>

1 个答案:

答案 0 :(得分:1)

好的,这里有一点假设,但我认为这是一个范围问题。您需要在文档就绪处理程序之外声明变量和函数,但是在其中附加按钮事件......

var selSound = '18000'; //default to first sound.
var play = false; //default to paused.

function selectSound(id) {
    selSound = id;
}

$(document).ready(function(){
    $('#playbtn').click(function() {
        var $this = $(this);
        var a = document.getElementById(selSound);   
        if ($this.text() == 'Zap!') {
            $this.children().children().text('Stop');
            a.play();
        } else {
            $this.children().children().text('Zap!');
            a.pause();
            alert('Stopped playing song: ' + selectedMP3);
        }
    });
})