Results 1 to 8 of 8

Thread: Customizing controls for the <audio> tag

  1. #1
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Customizing controls for the <audio> tag

    Good morning,

    I'm trying to create a short list of sound files, each with a <Play> icon beside it, when pressed the mp3 file plays and the icon changes to a <Pause> icon. I have the beginnings of it working but can't figure out two things:

    - how I should change the words play/ pause to be icons or images and
    - the code I have only works for a single entry and I'd like to have multiple entries on the same page. Below is the html and below that the js I have working so far

    Code:
     <audio id="yourAudio" preload='none'>
        <source src='http://dl.dropbox.com/u/1538714/article_resources/song.m4a' type='audio/mpeg' />
    </audio>
    <a href="#" id="audioControl">play!</a>&nbsp;Song 1<br />
        &nbsp;Song 2<br />
        &nbsp;Song 3<br />
    And here is the js I have

    Code:
      <script type="text/javascript">
    var yourAudio = document.getElementById('yourAudio'),
        ctrl = document.getElementById('audioControl');
    
    ctrl.onclick = function () {
    
        // Update the Button
        var pause = ctrl.innerHTML === 'pause!';
        ctrl.innerHTML = pause ? 'play!' : 'pause!';
    
        // Update the Audio
        var method = pause ? 'pause' : 'play';
        yourAudio[method]();
    
        // Prevent Default Action
        return false;
    };
    </script>
    The only controls I'd like are Play/ Pause

    I'd really appreciate any suggestions on this.

    Thanks

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    If you put all the control and audio tags into a parent div with an id, you can target that parent div and add the child tags to an array. Then you can increment through the child tags and apply your function to each. Restructure your markup a little bit and you can also target control/audio parings with the nextSibling property
    Code:
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Audio Controls</title>
    </head>
    <body>
    
    <div id="audio">
    
    <a href="#">Play!</a> : Song 1 - Takin' It All
    <audio preload="none">
        <source src="audio_player/audio/takin_it_all.mp3" type="audio/mpeg" />
    </audio><br/>
    
    <a href="#">Play!</a> : Song 2 - Too Close
    <audio preload="none">
        <source src="audio_player/audio/too_close.mp3" type="audio/mpeg" />
    </audio><br/>
    
    <a href="#">Play!</a> : Song 3 - Gettin' Down To Business
    <audio preload="none">
        <source src="audio_player/audio/gettin_down_to_business.mp3" type="audio/mpeg" />
    </audio><br/>
    
    </div>
    
    
    <script>
    var ctrl = document.getElementById('audio').getElementsByTagName('a');
    for(var i = 0; i < ctrl.length; i++){
             ctrl[i].onclick = function(){
    		var audio = this.nextSibling;
    		while (audio.nodeType != 1){ audio = audio.nextSibling; }
    		var pause = this.innerHTML === 'Pause!';
    		this.innerHTML = pause ? 'Play!' : 'Pause!';
    		var method = pause ? 'pause' : 'play';
    		audio[method]();
    		return false;
                 }
         }
    </script>
    
    </body>
    </html>
    Demo: http://fofwebdesign.co.uk/template/_...io-control.htm
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you Beverly (again)

    I think I follow what you were doing but couldn't have written it myself! Could I ask how would I change this so that instead of having text for Play & Pause if I used images for play/ pause buttons?

    Thanks so much for your time.

    Tony

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    OK - A version with icons can be found here: http://fofwebdesign.co.uk/template/_...trol-icons.htm

    Note the extra markup for the <img> tags, in both the web page body and as script variables.

    I also included some CSS to make the alignment look nicer.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. The Following User Says Thank You to Beverleyh For This Useful Post:

    tonybabb (01-06-2015)

  6. #5
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    I noticed that the text collapsed on the first click of one of the "Play" icons. I guess this was because the "Pause" icon hadn't been cached, because it never happened again. This could probably be overcome by including the icon width and height inside the image tags or putting both icons into a single file and using an offset.

  7. #6
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you so very much, you make it look easy !

    Tony

  8. #7
    Join Date
    Aug 2010
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    I didn't see this at all. What browser were you using?

  9. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by tonybabb View Post
    I didn't see this at all. What browser were you using?
    No problem on my machine either.

Similar Threads

  1. Customizing "Simple Controls Gallery" to support multiple galleries.
    By quarfie in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 03-07-2011, 11:08 PM
  2. Simple Controls Gallery v1.3 Problem with not Showing Images or Controls
    By macca85 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 08-13-2009, 04:29 PM
  3. Simple Controls Gallery - controls outside of images?
    By toolman in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 12-22-2008, 07:26 PM
  4. Replies: 1
    Last Post: 09-19-2007, 02:05 AM
  5. customizing embeded audio interface
    By rohis in forum HTML
    Replies: 0
    Last Post: 03-06-2006, 09:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •