Skip to content Skip to sidebar Skip to footer

Trying To Play Audio Using Javascript. But Is Not Playing?

So I am trying to make a radio station project just for fun. Where in the start it will play an audio file saying like: 'Welcome to the beat at 97.5' or whatever and then it would

Solution 1:

The code you have posted looks good, please check the javascript console that there are no errors and make sure that the path of the file is correct.

Other than that you also have multiple options that you can use to enhance the functionality.

SoundManager2

SoundManager2 is an excellent library that provides tons of features(and backward compatibility too if you prefer).

The sample code will be something like this.

<scriptsrc="/path/to/soundmanager2.js"></script><script>
soundManager.setup({
  url: '/path/to/swf-files/',
  onready: function() {
    var mySound = soundManager.createSound({
      id: 'aSound',
      url: '/path/to/an.mp3'
    });
    mySound.play();
  }
});
</script>

Howler.js

Howler too an excellent library thats worth looking.Its example code will be some thing like this

var sound = newHowl({
  urls: ['sound.mp3', 'sound.ogg', 'sound.wav'],
  autoplay: true,
  loop: true,
  volume: 0.5,
  onend: function() {
    alert('Finished!');
  }
});

Solution 2:

That code should work. Which browser are you using?

Alternative way with more options would be using this library http://goldfirestudios.com/blog/104/howler.js-Modern-Web-Audio-Javascript-Library

Post a Comment for "Trying To Play Audio Using Javascript. But Is Not Playing?"