Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with AudioContext with a source from audio tag

I'm building a music player and I'd like to add a pulsation effect depending on the track currently played.

Here some examples of what I'd want:

http://www.htmlfivewow.com/demos/hal/index.html

http://www.schillmania.com/projects/soundmanager2/demo/360-player/canvas-visualization.html

My problem is that in the first example, sound is loaded via xhr, and in the second it uses flash.

I'd want to be able to get the sound that I want to analyze from the audio tag.

I'm afraid it could not be possible, it would cause a big lack of security because we could load web pages instead of sound and then analyse it. Is there a solution anyway?

like image 783
Gaël Barbin Avatar asked Oct 25 '12 23:10

Gaël Barbin


People also ask

Can we embed an audio file with source tag?

To embed audio in HTML, we use the <audio> tag. Before HTML5, audio cannot be added to web pages in the Internet Explorer era. To play audio, we used web plugins like Flash.

How do you embed an audio link in HTML?

You can also use the <embed> tag or the newer <audio> tag to insert a sound file directly into a web page. With audio files, we recommend using the . MP3 file format because of its wide acceptance on the Internet, and is utilized by all browsers and operating systems.

Can audio tag work without control attribute?

Audio element shows just fine but as soon as you remove 'controls' from the audio tag, it disappears.


1 Answers

You can use createMediaElementSource method in AudioContext.

<audio id="audio" src="test.mp3"></audio>
<script type="text/javascript">
    var context = new webkitAudioContext;
    var el = document.getElementById('audio');
    var source = context.createMediaElementSource(el);
    source.connect(context.destination);
    el.play();
</script>
like image 64
David Shim Avatar answered Oct 05 '22 03:10

David Shim