Html 5 audio tag custom controls?

后端 未结 4 617
南方客
南方客 2020-12-02 17:27

I feel like I\'m taking crazy pills here because I can not figure out how to render the html 5 audio tag with custom controls.

I have this html so far, and it works

相关标签:
4条回答
  • 2020-12-02 17:45

    After a lot of research, I found an easy way of eliminating and manipulating specific parts of the predefined controls.

    Create your elements as you usually would, like so:

    <audio autoPlay>
        <source src='audioUrl' type='audio/mpeg' />
    </audio>
    

    Then in the CSS file, you write the following:

    /* Specifies the size of the audio container */
    audio {
      width: 115px;
      height: 25px;
    }
    
    audio::-webkit-media-controls-panel {
      -webkit-justify-content: center;
      height: 25px;
    }
    
    /* Removes the timeline */
    audio::-webkit-media-controls-timeline {
      display: none !important;
    }
    
    /* Removes the time stamp */
    audio::-webkit-media-controls-current-time-display {
      display: none;
    }
    audio::-webkit-media-controls-time-remaining-display {
      display: none;
    }
    

    With this code, you should get a small and nice looking container with only mute-button, pause/play-button and the 'download-file'-tag.

    For an overview of all the things you can modify, have a look here.

    The following code will also remove the mute- and the play-button:

    /* Removes mute-button */
    audio::-webkit-media-controls-mute-button {
      display: none;
    }
    
    /* Removes play-button */
    audio::-webkit-media-controls-play-button {
      display: none;
    }
    
    0 讨论(0)
  • 2020-12-02 17:54

    if you want the built-in play-button only you could:

    use autio-tag's class attribute:

     <audio controls preload='auto' class="audio_volume_only"><source src='the url to the 
    audio' type='audio/mp3' /></audio>   
    

    and fit the css:

     .audio_volume_only {
        width: 35px;
      }
    

    i hoped controls has some parameters, but not found any or misunderstood .. we'll see.

    then possibly use audio's onplay - event to change the css to your need. the play-button will become the pause-button in built-in controls

    as others pointed out, you can always make your own....

    0 讨论(0)
  • 2020-12-02 17:59

    You create your elements like so...

    <audio id="yourAudio" preload='none'>
        <source src='the url to the audio' type='audio/wav' />
    </audio>
    <a href="#" id="audioControl">play!</a>
    

    And add some functionality:

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

    Right now, the button is just simple text ("play!" or "pause!"), but you could do just about anything you wanted with CSS. Instead of setting the innerHTML, set the className and you're good to go!

    0 讨论(0)
  • 2020-12-02 17:59

    I have been experimenting with the use of a graphic instead of the player. Set the style within the 'audio' tag to "display: none; width: 0px; height: 0px;" (display none does not work on iPad, thus the additional width-0 and height-0 settings). Also not including the "controls" attribute should work. (different systems/browsers & desktop vs iOS all act differently.....)

    Eg:

    <head>
    <script>
    function EvalSound(soundobj) {
      var thissound=document.getElementById(soundobj);
      thissound.play();
    }
    </script>
    </head>
    
    <body>
    Click the speaker to hear the sound.  <a href="javascript:null()" onClick="EvalSound('sound1'); return false;">
    <img src="sound_icon.png" alt="" title="sound_icon" width="" height="" class="" /></a>
    <audio id="sound1" style="display: none; width: 0px; height: 0px;" src="sound.mp3" controls preload="auto" autobuffer>
    </body>
    

    The "javascript:null()" works on iPad in conjunction with "return false;" as opposed to the usual "#".

    For more really useful information: http://www.phon.ucl.ac.uk/home/mark/audio/play.htm

    0 讨论(0)
提交回复
热议问题