Click here to Skip to main content
15,176,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I want to use wmp but with [DllImport()].
I am not familiar to this code, how can I play a sound in this way?
Posted
Updated 7-Dec-10 23:26pm
v3
Comments
senguptaamlan 8-Dec-10 3:54am
   
what do you actually want to do ??? please provide the functional requirement....usually DLLIMPORT is not very good way to do the things...so that if there is any other possibility, it can be suggested
Dalek Dave 8-Dec-10 3:59am
   
Edited for Readability.

1 solution

DllImport isn't really necessary for playing sounds anymore.

If you really need to use DllImport to use the unmanaged sound APIs take a look at this article: Playing .wav files using C#[^]. (Note that this article is written with .Net 1.1 and there are much easier ways of playing sound now in the newer versions of the framework)

If you are just playing .wav files and don't need to use windows media player, you could just do:
System.Media.SoundPlayer player = new System.Media.SoundPlayer("SourceFile.wav");
player.Play();
If you want to play other media types or actually want to play sounds through windows media player you first add a reference to C:\Windows\System32\wmp.dll and then use this code:
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayerClass();
wmp.URL = "SoundFile.wav";
wmp.controls.play();
This supports any file type that media player supports.

Finally, you could use the
XNA framework Media namespace[^]. This supports most of the common formats, but the downside is you'll need to reference the XNA framework libraries:
Uri songFile = new Uri("SoundFile.mp3");
Song yourSong = Song.FromUri("YourSong", songFile );
MediaPlayer.Play(yourSong);
   
v2
Comments
mehdi_k 8-Dec-10 7:57am
   
Thanks,but it's for .wav files , i want to play any sound file.
can u guide me?
Simon P Stevens 8-Dec-10 9:03am
   
The windows media player example will work with any file type that windows media player supports. I've also updated the answer to include an XNA media example which will support other formats.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900