jquery - HTML5 Audio "paused" property returns true no matter what -
i trying make toggle play function, using if statement. use .paused
method/property checking if <audio>
element playing. problem property returns true if audio playing. here's code:
var player = $("#player"); var playere = $("#player")[0]; var playere = $("#player").get(0); var ispaused = playere.paused; function toggleplay() { if (ispaused === false) { player.trigger("pause"); } else { player.trigger("play"); } }
here's audio element too:
<audio id="player" src="source added dynamically" controls="controls" preload="none"></audio>
if need test out, here's snippet:
var player = $("#player"); var playere = $("#player")[0]; var playere = $("#player").get(0); var ispaused = playere.paused; function toggleplay() { if (ispaused === false) { player.trigger("pause"); } else { player.trigger("play"); } } /** added testing **/ function check() { alert(ispaused); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <audio id="player" src="http://98678871.ngrok.io/music/test.mp3" controls="controls" preload="none"></audio> <button onclick="check()">check isplaying</button>
as can see, returns true if audio playing. going on here? bug?
there's difference between reference, , value.
if var ispaused = playere.paused;
it's same saying var ispaused = true;
. ispaused isn't going change when playere.paused changes.
thus, when check paused state, must use playere.paused.
var player = $("#player"); var playere = $("#player")[0]; var playere = $("#player").get(0); var ispaused = playere.paused; function toggleplay() { if (ispaused === false) { player.trigger("pause"); } else { player.trigger("play"); } } /** added testing **/ function check() { alert(playere.paused); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <audio id="player" src="http://98678871.ngrok.io/music/test.mp3" controls="controls" preload="none"></audio> <button onclick="check()">check isplaying</button>
you can listen when video/audio pauses/plays , update variable based on that.
var player = $("#player"); var playere = $("#player")[0]; var playere = $("#player").get(0); var ispaused = true; player.bind("pause", function() { ispaused = true; }); player.bind("play", function() { ispaused = false; }); /** added testing **/ function check() { alert(ispaused); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <audio id="player" src="http://98678871.ngrok.io/music/test.mp3" controls="controls" preload="none"></audio> <button onclick="check()">check isplaying</button>
Comments
Post a Comment