Run the next sound in Javascript?

Asked

Viewed 936 times

3

I have an audio folder with voice/words saved in wave and I want to run Javascript in different cases. For example: "Error logging in. Please try again". I found some code on the web but they do not run the audios one by one. Here on Stack I found this code -posted by the flash user- which apparently approaches more than I need:

function loadPlayer() {
    var audioPlayer = new Audio();
    audioPlayer.controls="controls";
    audioPlayer.addEventListener('ended',nextSong,false);
    audioPlayer.addEventListener('error',errorFallback,true);
    document.getElementById("player").appendChild(audioPlayer);
    nextSong();
}
function nextSong() {
    if(urls[next]!=undefined) {
        var audioPlayer = document.getElementsByTagName('audio')[0];
        if(audioPlayer!=undefined) {
            audioPlayer.src=urls[next];
            audioPlayer.load();
            audioPlayer.play();
            next++;
        } else {
            loadPlayer();
        }
    } else {
        alert('the end!');
    }
}
function errorFallback() {
        nextSong();
}
function playPause() {
    var audioPlayer = document.getElementsByTagName('audio')[0];
    if(audioPlayer!=undefined) {
        if (audioPlayer.paused) {
            audioPlayer.play();
        } else {
            audioPlayer.pause();
        }
    } else {
        loadPlayer();
    }
}
function pickSong(num) {
    next = num;
    nextSong();
}

var urls = new Array();
    urls[0] = 'audio.mp3';
    urls[1] = 'audio.mp3';
    urls[2] = 'audio.mp3';
    urls[3] = 'audio.mp3';
    urls[4] = 'audio.mp3';
    urls[5] = 'audio.mp3';   
    urls[6] = 'audio.mp3';       
var next = 0;

I want to make it clear that I’m doing this just for learning. I recently graduated in Computer Technician for internet and I’m loving the programming area. :)

  • 2

    What does this code do and why doesn’t it answer? What error does it give or function does it doubt? Please, instead of responding in the comments, click [Edit] and add details to the question.

1 answer

2


Planex,

There are some projects that can help you make a very good player using only Javascript and angular;

But for your immediate problem use HTML5 I will show you. I will use Jquery to make it easy

//Coloque um audio no seu corpinho de HTML
$('<audio id="chatAudio">
    <source src="ERROU.ogg" type="audio/ogg">
    <source src="ERROU.mp3" type="audio/mpeg">
</audio>').appendTo('body');

Then we play in an event selector:

//Errou a senha produção
$('#alerta').show{
$('#chatAudio')[0].play();
}

Or do it in direct Javascript :

<!DOCTYPE HTML>
  <html>
  <head>
  <title>Audio</title>
  </head>
  <body>

    <script>
  function play(){
       var audio = document.getElementById("audio");
       audio.play();
                 }
   </script>

<input type="button" value="PLAY"  onclick="play()">
<audio id="audio" src="http://dev.interactive-creation-works.net/1/1.ogg" ></audio>
 </body>
 </html>
  • 1

    Thank you. I managed to run through the audio files I contain (VOX Half-Life). Each audio is now running at different events. For example: Wrong.mp3 and password.mp3. Valeu, Dom.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.