Reputation: 21
I have no idea what I'm doing wrong
<script>
$(document).ready(function() {
var playing = false;
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'star.mp3');
$('#star').click(function() {
playing == true;
if(playing == true) audioElement.pause();
else audioElement.play();
});
});
What it is meant to do is that when one sound is playing and I click on the other sound the sound that already plays should stop and the other should start.
Upvotes: 1
Views: 66
Reputation: 16170
I think your problem is here:
$('#star').click(function() {
playing == true; // should be = not == if you're trying to assign
if(playing == true) audioElement.pause(); // but if you assign then playing is always true
else audioElement.play();
});
See assignment operators
and comparison operators
Here's a simplified version that may help:
$(document).ready(function() {
var audio1 = $("#DunDunDun")[0];
var audio2 = $("#WaWaWaa")[0];
$("#ominousMusic").click(function() {
audio1.play();
audio2.pause();
});
$("#sadMusic").click(function() {
audio2.play();
audio1.pause()
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ominousMusic" title="go on, click it, you know you want to...">
cue ominous music...
</div>
<audio id="DunDunDun">
<source src="http://pluggedinwebdesign.com/media/DunDunDun.mp3">
Your browser isn't invited to super fun audio time.
</audio>
<div id="sadMusic" title="go on, click it, you know you want to...">
cue sad music...
</div>
<audio id="WaWaWaa">
<source src="http://pluggedinwebdesign.com/media/Sad_Trombone.mp3">
Your browser isn't invited to super fun audio time.
</audio>
Upvotes: 1