Trying to add a mp3 sound to a pressed button Trying to add a mp3 sound to a pressed button

I am as green as with Javascript but I am just wanting the page below to change the photo (which it does) update the text (which it does) and play the song of the bird once the bird name is pressed. I have it playing one song with the use of the "Play" button, then I tried to move parts of that in to each function, no joy. I have tried to an another getelementbyid (audio) but that didn't work, I am stuck when trying to have it run from within each function.

I hope that is kind of clear and thanks for any help in advance.

<html>
<head>
</head>
<body>
    <p>
        <h2>Click the button to change the bird image.</h2>
    </p>
    <img id="myImg" src="http://www.outgrabe.net/bird04.jpg" width="207" height="198">
    <p id="p1">Rose Robin by JJ Harrison (CC-by-SA)</p>
    <BR></BR>
    <button onclick="myFunction_1()">Pardalote</button>
    <button onclick="myFunction_2()">Purple Swamp Hen</button>
    <button onclick="myFunction_3()">White-headed Stilt</button>
    <button onclick="myFunction_4()">Inland Thornbill</button>
    <button onclick="myFunction_5()">Rose Robin</button>
    <input type="button" value="PLAY" onclick="play()">
    <audio id="audio" src="https://amitylabradoodles.com.au/wp-content/uploads/2021/07/Pardalote.mp3"></audio>
    <script>
        function myFunction_1() {
            document.getElementById("myImg").src = "http://www.outgrabe.net/bird00.jpg";
            document.getElementById("p1").innerHTML = "Pardalote by fir0002 (CC-by-NC)";
        }
        function play() {
            var audio = document.getElementById("audio");
            audio.play();
        }
        function myFunction_2() {
            document.getElementById("myImg").src = "http://www.outgrabe.net/bird01.jpg";
            document.getElementById("p1").innerHTML = "Purple swamp hen by Toby Hudson (CC-by-SA)";
        }
        function myFunction_3() {
            document.getElementById("myImg").src = "http://www.outgrabe.net/bird02.jpg";
            document.getElementById("p1").innerHTML = "White-headed Stilt by JJ Harrison (CC-by-SA)";
        }
        function myFunction_4() {
            document.getElementById("myImg").src = "http://www.outgrabe.net/bird03.jpg";
            document.getElementById("p1").innerHTML = "Inland Thornbill by Peter Jacobs (CC-by-SA)";
        }
        function myFunction_5() {
            document.getElementById("myImg").src = "http://www.outgrabe.net/bird04.jpg"
            document.getElementById("p1").innerHTML = "Rose Robin by JJ Harrison (CC-by-SA)";;
        }
        /* The links to the MP3 flies 
        Pardalote
        https://amitylabradoodles.com.au/wp-content/uploads/2021/07/Pardalote.mp3   
        Purple Swamp Hen
        https://amitylabradoodles.com.au/wp-content/uploads/2021/07/Purple-Swamphen.mp3
        White-headed Stilt
        https://amitylabradoodles.com.au/wp-content/uploads/2021/07/White-backed-Stilt-bird-sounds.mp3
        Inland Thornbill
        https://amitylabradoodles.com.au/wp-content/uploads/2021/07/Inland-Thornbill.mp3
        Rose Robin
        https://amitylabradoodles.com.au/wp-content/uploads/2021/07/Rose-Robin.mp3 */
   }
    </script>
</body>
</html>


from Stackoverflow

Comments

Popular posts from this blog

How to extract items from a subarray and push them to the main array

Detox gets stuck on JS Timer/Main Run Loop when registering a lot of screens with React Native Navigation Detox gets stuck on JS Timer/Main Run Loop when registering a lot of screens with React Native Navigation

\\u0000 cannot be converted to text error \\u0000 cannot be converted to text error