Harry Vella-Thompson
Harry Vella-Thompson

Reputation: 39

How can I play audio from a URL in the client-side ASP.NET?

I am creating a page in an ASP.NET (.NET Framework Version 4.0, ASP.NET Version 4.7) web application and I need to be able to play audio from a server

The URL is like this: http://xx.xx.xxx.x/.global/call_recording_archive/download.php?file=xxxxxxxxxxxxxx-all.mp3 but I can't work out any way that I can get this to stream to the audio player in the browser.

Any help would be appreciated.

Upvotes: 1

Views: 1549

Answers (1)

ggordon
ggordon

Reputation: 10035

One simple way is by using javascript and the DOM api you can create a hidden audio node and call the play method in javascript.

Step 1

You can do this dynamically in javascript or simply place a hidden audio node in your html


<audio id="audioPlayer" style="display:none" autoplay=false>
  <source id="audioSrc" src="foo.wav" type="audio/mp3">
  Your browser does not support the <code>audio</code> element. 
</audio>

Step 2

After receiving your stream you can set the source of the audio element

var audioSrc = document.getElementById("audioSrc")
audioSrc.setAttribte("src","http://xx.xx.xxx.x/.global/call_recording_archive/download.php?file=xxxxxxxxxxxxxx-all.mp3");

Step 3

Now that you have an audio element you can call the play method on that element in your browser

var audioPlayer = document.getElementById("audioPlayer")
audioPlayer.play()

Additional Notes

There are other methods eg stop , pause

You can even have more control over the AudioContext have a look at this article

Upvotes: 1

Related Questions