Остановка звуковой дорожки при щелчке мышью - PullRequest
0 голосов
/ 18 ноября 2018

Я изучаю некоторые разработки для начинающих. Я создал очень простой сайт, который использует комплект Lightbox. Я разработал, как запустить звуковую дорожку, когда кто-то щелкает по миниатюре, но ему нужны некоторые указатели о том, как ее остановить, или для более надежного решения, проиграйте соответствующую дорожку, если кто-то нажмет стрелку «далее». Вот мой код:

<!doctype html>

<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="css/lightbox.min.css">
    <script src="js/lightbox-plus-jquery.min.js"></script>

    <meta charset="utf-8">
    <title>verenti ltd</title>
    <meta name="viewport" content="width-device-width, initial-scale=1.0">
    <meta name="author" content="SitePoint">

</head>

<body bgcolor="#F7FBED  ">

<div class="gallery">
    <a href="images/drake_big.jpg" data-lightbox="mygallery" data-title="nick drake"
       onclick=PlaySound("music/drake.mp3")><img src="images/drake_small.jpg"></a>
    <a href="images/prine_big.jpg" data-lightbox="mygallery" data-title="john prine"
       onclick=PlaySound("music/prine.mp3")> <img src="images/prine_small.jpg"></a>
    <a href="images/westerberg_big.jpeg" data-lightbox="mygallery" data-title="paul westerberg"
       onclick=PlaySound("music/westerberg.mp3")> <img src="images/westerberg_small.jpg"></a>
    <a href="images/townes_big.png" data-lightbox="mygallery" data-title="townes van zandt"
       onclick=PlaySound("music/townes.mp3")> <img src="images/townes_small.jpg"></a>
    <script>
        function PlaySound(path) {
            //this function will work like a toggler for sound track playing
            var sound = document.getElementById(path);
            if(sound && sound.currentTime > 0){ //check whether it is already playing
                sound.pause();// stop the sound
                sound.currentTime = 0 //
            }else if(sound){
                //this block to play paused sound track
                sound.play();
            }else{
                //this block to play new sound track
                sound= document.createElement('audio');
                sound.setAttribute('src', path);
                sound.setAttribute('id', path);
                sound.play();
            }
        }

    </script>
</div>
</body>
</html>

Я думаю, я мог бы лучше использовать тег?

1 Ответ

0 голосов
/ 18 ноября 2018

Простое решение будет:

    <script>
    function PlaySound(path) {
            //this function will work like a toggler for sound track playing
            var sound = document.getElementById(path);
            if(sound && sound.currentTime > 0){ //check whether it is already playing
               sound.pause();// stop the sound
               sound.currentTime = 0 //
            }else if(sound){
                 //this block to play paused sound track

               sound.play();
            }else{
                 //this block to play new sound track

                  sound= document.createElement('audio');
                  sound.setAttribute('src', path);
                  sound.setAttribute('id', path);
                  sound.play();
            }


    }

</script>
...