0

This is a more detailed version of my previous unanswered question: https://stackoverflow.com/questions/26076274/html-css-toggle-mute-with-picture-assignment-button

I need some help with some CSS/html5 scripting. You see, I've got this pretty cool button that mutes/unmutes audio, and the button itself toggles between two pictures (one for mute, one for unmute. You know, the basic mute/unmute speaker icons) depending on it's current state. The only problem is that I can't figure out how to make this button work. The button displays perfectly on my page and everything, and when I click on it, the picture changes like its suppose to, but my track is still playing. I already know what the problem is. You see, I don't know how to assign this button to my audio file. The audio itself has lots of attributions (autoplay, looping, display:none etc.), so I don't know quite where in my script to put it. The script for the button was found on a previous StackExhange thread (answer by Jaka Dirnbek): How to create a only mute/unmute button (like youtube) in html and the asker of this question asked only for the button, and not the audio setup, and thats where I'm stuck.

Where do i put my audio src?

In the head? or in the body? and how do i write my audio code? I currently have the following script for my audio:

<audio id="track" src="Poopsyflop.wav" loop="loop" autoplay="autoplay" div style="display:none">

¨

If anyone has any ideas, I would love to hear them :) Thanks in advance!

-Daniel

Community
  • 1
  • 1

2 Answers2

0

You assign it using the audio file ID

DEMO http://jsfiddle.net/mattball/sVwXH/

HTML

// audio file
<audio id="background_audio" autoplay="autoplay">
  <source src="http://www.jplayer.org/audio/m4a/Miaow-07-Bubble.m4a" />
</audio> 

// button
<a href="#noscroll" id="mute">toggle sound</a>

JS

var audio = document.getElementById('background_audio');

document.getElementById('mute').addEventListener('click', function (e)
{
    e = e || window.event;
    audio.muted = !audio.muted;
    e.preventDefault();
}, false);

This is based on an example that is readily available online through various sources.

Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37
  • Sadly though, my resources are limited. I don't have javascript :/ Do you think that would be possible through only using css and html5? and still keep the picture toggle effect? Thank you very much though! :) I really appreciate your help – Daniel Thorstensen Sep 28 '14 at 19:19
  • You can not do this with CSS only. Why can't you apply javascript? My example is not using jQuery. You would not need to refer to a 3rd party library. Just simply include this javascript into the footer of your document. assign the ID of the audio file and the ID of the button and it should all fall into place. – Kevin Lynch Sep 28 '14 at 19:22
  • Wait, are you saying I don't need to download any javascript program to use javascript codes? I talked to my IT teacher about this, and he told me I need to buy javascript.. wow.. I'm stupid.. I really apologize mate. Thanks alot for your help! much apprechiated! :) One more stupid question from a new scripter though. I'm not sure how footers work. I've gone through a couple of courses on codecademy, but haven't got that far yet. If I'm not mistaken, a footer is a tag added between the body tags, right? so all i have to do is surround the javascript part with two footer tags? – Daniel Thorstensen Sep 28 '14 at 19:28
  • You don't need to purchase javascript or download any kind of program. You simply write the code and a browser will run it. There are different libraries out there that make writing javascript easier, but in this instance it is a very simple method, no library is required. With regards placing it inside the footer, that is just a recommendation to assist in ensuring the page loads quicker. Just place my code example before the closing `

    ` tag and it will be fine.

    – Kevin Lynch Sep 28 '14 at 19:33
  • I didn't know that.. I'm sorry for my incompitence. so i got the mute effect to work, but the picture toggle effect is gone. any idea how to combine what you've shown me with the toggle button I already have? – Daniel Thorstensen Sep 28 '14 at 19:36
  • That's ok, we have all had to learn at some point. Have you changed the ID of the button itself or did you change the ID within the JavaScript? If you changed the ID if the button then this could stop your CSS targeting it. I suggest changing the ID within the JavaScript to match what ID you have on the button currently. – Kevin Lynch Sep 28 '14 at 19:41
  • I don't quite follow. I copied your codes and replaced your IDs with mine, but now I have 2 seperate buttons, one that mutes and unmutes my audio, and the one that toggles the mute icons. – Daniel Thorstensen Sep 28 '14 at 19:44
  • The button that toggles the mute icons, use that ID in the JavaScript as you only need and want a single button. That should then work. – Kevin Lynch Sep 28 '14 at 19:47
  • But the button consists of two codes, One of which I have placed in between my head tags, and one that's between my body tags. How do I put them in the javascript? If you like, I can send you the whole script. I'm still learning to script, so it's probably quite messy. – Daniel Thorstensen Sep 28 '14 at 19:50
  • If you send me the script I could have a quick look – Kevin Lynch Sep 28 '14 at 19:51
  • great :) should I send it via email? or as a comment? (Not sure if the structure will stay in the comment though) – Daniel Thorstensen Sep 28 '14 at 19:53
  • I just had a look at your code (I'm on an iphone so not that easy to read code) but it seems that the JavaScript in my example can be used if you change 'mute' to 'un-mute' and 'background_audio' to 'track' – Kevin Lynch Sep 28 '14 at 20:19
  • Do you then suppose I keep my old toggle button, and they will automaticly link? and i tried putting in your code, but I'm not sure what tags to surround it with, and if i just put it in before i close the body tag, the code appears as text on my page – Daniel Thorstensen Sep 29 '14 at 04:21
  • You need to wrap my example code in `script` tags e.g. `` – Kevin Lynch Sep 29 '14 at 05:30
  • Okay, thanks! :) I'll give that a go! I'll tell you how it goes in a minute – Daniel Thorstensen Sep 29 '14 at 05:50
  • Ok, now the mute button is in fact my toggle mute picture button, but the pictures aren't toggeling on click :s did i do something wrong? – Daniel Thorstensen Sep 29 '14 at 05:56
0

Take a look here.

Example:

.unmute{
  background-image: url(http://franriavilla.in/images/unmute.png);
  background-size: cover;
  width:35px;
  height:30px;
  cursor: pointer;
  }

.mute{
  background-image: url(http://franriavilla.in/images/mute.png);
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<audio id="track" autoplay="autoplay" loop="loop">
        <source src="http://www.w3schools.com/html/horse.ogg" type="audio/ogg" />
    </audio> 
    
<div class='unmute' onclick="document.getElementById('track').muted = !document.getElementById('track').muted;$(this).toggleClass('mute')"></div>
Community
  • 1
  • 1
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • It works! :D though the picture toggle effect is gone. Is there any way I can combine my button with this? I don't have javascript, so I can only use html5 and CSS – Daniel Thorstensen Sep 28 '14 at 19:18
  • no js? hmmm... change button image without js possible only on postback using server side code. – aleha_84 Sep 29 '14 at 06:49
  • I apologize, i actually thought i needed to buy something in order to use javascript, my fault. Javascript is fine :) though I don't know how to use it – Daniel Thorstensen Sep 29 '14 at 06:52
  • @DanielThorstensen here is working example with button mute/unmute image toggle. Warning: sound loop! – aleha_84 Sep 29 '14 at 07:47
  • I placed it onto my script and the track loops and autoplays and everything, but the button doesn't appear. Could you assist me with placing your codes in my script? thanks – Daniel Thorstensen Sep 29 '14 at 08:08
  • `loop="loop" autoplay="autoplay"` taken from your ansver. Button should appear, or you don't have access to example images taken from site mentioned in my ansver. Check that you copied all markup. And are there any errors in dev console (F12)? – aleha_84 Sep 29 '14 at 08:13
  • Well, i tried replacing your image sources with my local images, but I don't know how to script that. As of right now, I have background-image: ; Perhaps that's where the problem comes from? – Daniel Thorstensen Sep 29 '14 at 08:34
  • I tried putting in your code in a new html code, and the button still doesn't appear for me. Is the first part suppose to be covered with script tags, and placed in the head? or is the second part suppose to be in the same script? I don't know how to set this up – Daniel Thorstensen Sep 29 '14 at 09:03
  • not familiar with html,css, js? unmute and mute should be wrapped in ` – aleha_84 Sep 29 '14 at 09:23
  • Okay, it works when I put it between the style tags, but I can't manage to make the url sources into my local files. how do i write it? My files are "Mute.png" and "unmute.png", but the file you've used are urls – Daniel Thorstensen Sep 29 '14 at 09:31
  • Ok i made it work :) but I have an odd problem. It works when I open the html file, but when I put my webpage online, the button doesn't appear. Have I messed up the sources somehow? I've added my photos to the folder I uploaded to my database, but still nothing – Daniel Thorstensen Sep 29 '14 at 11:27