0

I'm using SoundPlayer object to play audio on my web application in c# now. But when I tried to play .mp3 file it wasn't successful. How can I play mp3 file on a c# web application from a url? I'm having the url of the audio to be played.

This is the code I'm using to play .wav audio file, but it fails for .mp3 files.

SoundPlayer player = new SoundPlayer();
 string sound url ="http://audio.oxforddictionaries.com/en/mp3/ranker_gb_1_8.mp3";

  player.SoundLocation = soundurl;

  player.Play();

  System.Threading.Thread.Sleep(2000);

  player.Stop();
iamal
  • 159
  • 2
  • 15

4 Answers4

1

If you open a documentation page for SoundPlayer (https://msdn.microsoft.com/en-us/library/system.media.soundplayer(v=vs.110).aspx) you'll read, that this class "Controls playback of a sound from a .wav file." This means, that if you want to play .mp3 you need something different, something capable of doing the job. There are a lot of options, to name a few: https://github.com/filoe/cscore, https://www.ambiera.com/irrklang/, https://github.com/naudio/NAudio and many more others.

Another thing is that you are working with ASP.Net. This means that what you probably want is to play music on the client's machine, not at your server like you do now. If this is the case, then this is a completely different story, no .Net sound libraries would help you, you need to learn what is the difference between client-side and server-side execution first, until you do that you don't go anywhere.

rs232
  • 1,248
  • 8
  • 16
  • client's browser won't support these ? My browser is playing it now and will it cause error on clients machine? – iamal Mar 19 '18 at 09:30
  • I guess you have both your server and your client hosted on the same machine now. When you'll have someone else connecting to your website from elsewhere, they will not hear a thing, while you will suddenly hear the music playing. – rs232 Mar 19 '18 at 09:42
  • Client's browsers support playing music. It works in a completely different way, though, see this question https://stackoverflow.com/questions/8641355/playing-audio-in-html for an example. – rs232 Mar 19 '18 at 09:44
  • Thank you, so how can I programmatically change the value of the audio url on HTML? – iamal Mar 19 '18 at 09:50
  • The Ultimate Task of ASP.Net, the whole sense of its existence, is the ability to programmatically change a value on HTML. Any good tutorial on ASP.Net would tell you how to do this. – rs232 Mar 19 '18 at 09:54
0

It looks like SoundPlayer is designed to play wav files only from this documentation.

You can use follow the steps provided in the following forum. It has a few steps that you can follow for playing mp3 files.

Or you could also try HTML5 Audio tag to embed mp3 in your application.

Anshuman
  • 1
  • 1
0

One important thing you should know: System.Media.SoundPlayer class will play the sound on server-side instead of client-side. This is indistinguishable if you're running the project locally since your machine plays it, but you should able to recognize something going wrong when trying to access the page in another client.

As far I as know, you need to use either <audio>, <object> or <embed> HTML tags to play MP3 from a URL in client browser such like examples below:

<!-- using embed tag -->
<embed width="100px" height="100px" src="http://audio.oxforddictionaries.com/en/mp3/ranker_gb_1_8.mp3" autoplay="true" autostart="true" />

<!-- using object tag -->
<object width="100px" height="100px" data="http://audio.oxforddictionaries.com/en/mp3/ranker_gb_1_8.mp3"></object>

<!-- using audio tag (HTML5) -->
<audio width="100px" height="100px" src="http://audio.oxforddictionaries.com/en/mp3/ranker_gb_1_8.mp3" type="audio/mp3"></audio>

Similar issue:

How do I play a sound in an asp.net web page?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

Create an empty html and run the music and check loop-auto start, then put that file on any server. then connect the server on project with WebBrowser