Javascript play sound on hover. stop and reset on hoveroff

后端 未结 3 426
别那么骄傲
别那么骄傲 2020-12-09 23:11
function EvalSound(soundobj) {
    var thissound=document.getElementById(soundobj);
    thissound.currentTime = 0;  
    thissound.Play();
}

function StopSound(soun         


        
相关标签:
3条回答
  • 2020-12-09 23:27

    The <embed> tag is the old way to embed multimedia. You really ought to be using the new HTML5 <audio> or <video> tags as they are the preferred and standardized way to embed multimedia objects. You can use the HTMLMediaElement interface to play, pause, and seek through the media (and lots more).

    Here is a simple example that plays an audio file on mouseover and stops it on mouseout.

    HTML:

    <p onmouseover="PlaySound('mySound')" 
        onmouseout="StopSound('mySound')">Hover Over Me To Play</p>
    
    <audio id='mySound' src='http://upload.wikimedia.org/wikipedia/commons/6/6f/Cello_Live_Performance_John_Michel_Tchaikovsky_Violin_Concerto_3rd_MVT_applaused_cut.ogg'/>
    

    Javascript:

    function PlaySound(soundobj) {
        var thissound=document.getElementById(soundobj);
        thissound.play();
    }
    
    function StopSound(soundobj) {
        var thissound=document.getElementById(soundobj);
        thissound.pause();
        thissound.currentTime = 0;
    }
    

    For more information, check out the MDN guide for embedding audio and video

    0 讨论(0)
  • 2020-12-09 23:38
    <html>
    
    <script>
    function stopAudio() {                      
    player.pause();
    player.currentTime = 0;           
    }
    </script>
    
    
    <body>
    <audio id="player" src="(place audio here)"></audio>
    
    <div> 
    <button onclick=" stopAudio()">Stop</button>
    </div>
    
    </body>
    </html>
    

    //End Note: you must look at your audio id as mine is named "player" this is the reason it is not working look at your css and your html very closely in your lines. The other thing you must be careful about is your call function as mine is stated as stopAudio() yours could be named different. The function only works with css if you use your call method properly.

    0 讨论(0)
  • 2020-12-09 23:44

    I had the same question, about starting and stopping audio. I use jQuery without any other plugins. My code is for mousedown and mouseup, but could be changed to other actions.

    HTML

    <div class="soundbutton">
       cool wind sound
    </div>
    <audio id="wind-sound" src="wind-sound/wind.mp3">
    

    Javascript

    $('.soundbutton').on('mousedown', function () {     
        playWind();                                  //start wind sound
    })
    .on('mouseup', function () {                    
        stopWind();                                  //stops the wind sound
    });
    
    
    // my full functions to start and stop
    
    function playWind () {                           //start wind audio
      $('#wind-sound')[0].volume = 0.7;
      $('#wind-sound')[0].load();
      $('#wind-sound')[0].play();
    }
    function stopWind () {                           //stop the wind audio
      $('#wind-sound')[0].pause();
      $('#wind-sound')[0].currentTime = 0;           //resets wind to zero/beginning
    }
    
    0 讨论(0)
提交回复
热议问题