Jimbly2
Jimbly2

Reputation: 227

Javascript audio play/stop

still having problems - if I get my code to play the selected audio the buttons won't toggle. If I include $(document).ready(function(){ then the button toggles but the audio won't play! Can anyone save the little hair I have left? This is an HTML5 jQuery Mobile project for delivery on iOS 5

Thanks for any/all help J

code:

$(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>

Upvotes: 1

Views: 2197

Answers (1)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

Okay, a bit of an assumption here, but I'm thinking it's a scope issue. You need to declare the variables and function outside the document ready handler, but attach the button events inside it...

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

Upvotes: 1

Related Questions