Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference URL with JavaScript to play sound?

Im using soundcloud dot com to upload my sounds. i want to press a button within my mobile application and have that sound play.

So basically i want my sound to be referenced from the URL that I am given when the button is pressed by the user.

I need to do it with Javascript only. No HTML 5 please. Any help is greatly appreciated cause this is Xtremely frustrating. Any ideas? Thanks in advance.

like image 537
JColling Avatar asked Apr 13 '12 14:04

JColling


People also ask

How can I play audio from a website?

To play sound file in the background on a web page, use the <embed>… </embed> element. Also, use the autoplay attribute. This will run music in the background whenever the page loads.

How do I add music to a JavaScript file?

You can create a JavaScript that allows the user to play a music or an audio file. In order to play an audio file in a browser, you need to use the <audio> tag. The audio object is new in HTML5. The <audio> element has a number of properties, one of them is controls.

What is JavaScript audio?

Audio() The Audio() constructor creates and returns a new HTMLAudioElement which can be either attached to a document for the user to interact with and/or listen to, or can be used offscreen to manage and play audio.


1 Answers

It's pretty simple to get started really:

function playSound(url) {
    var a = new Audio(url);
    a.play();
}

Use that as whatever event handler you want for your application. Of course, I'd hope you'd want to do more than just play (for example, maybe pause would be good too?), but it's a start.

like image 87
nickf Avatar answered Oct 03 '22 21:10

nickf