How do I play mp3 file in .Net? I have read that using DirectX/openGL will do the trick. So I hope JR will help me through this.:D
Btw Im using 1.0 framework.
Printable View
How do I play mp3 file in .Net? I have read that using DirectX/openGL will do the trick. So I hope JR will help me through this.:D
Btw Im using 1.0 framework.
I'm sure you can use DirectX, but you can also use MCI via Windows API, or reference the Windows Media Player COM library and use a WindowsMediaPlayerClass object, which is like the ActiveX control but without the UI.
got any good article/tutorial about that?
top of your code window ...
to use, a simple bit of code that allows you to open / play / stop an mp3 i put together ...VB Code:
[COLOR=Green]/// add these 2 references ... [/COLOR] [COLOR=Blue]using[/COLOR] System.Runtime.InteropServices; [COLOR=Blue]using[/COLOR] System.Text;
hope it helps :)VB Code:
[DllImport("winmm.dll", EntryPoint="mciSendStringA")] [COLOR=Blue]private static extern int [/COLOR] mciSendString ([COLOR=Blue]string[/COLOR] lpstrCommand, [COLOR=Blue]string[/COLOR] lpstrReturnString, [COLOR=Blue]int[/COLOR] uReturnLength, [COLOR=Blue]int[/COLOR] hwndCallback); [DllImport("kernel32.dll", EntryPoint="GetShortPathNameA")] [COLOR=Blue]private static extern int[/COLOR] GetShortPathName ([COLOR=Blue]string[/COLOR] lpszLongPath, StringBuilder lpszShortPath, [COLOR=Blue]int[/COLOR] cchBuffer); [COLOR=Blue]private string[/COLOR] Alias = "MP3"; [COLOR=Blue]private void[/COLOR] btnplay_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e) { OpenFileDialog od = [COLOR=Blue]new[/COLOR] OpenFileDialog(); od.Filter = "MP3 Audio(*.mp3)|*.mp3"; od.RestoreDirectory = [COLOR=Blue]false[/COLOR]; [COLOR=Blue]if[/COLOR](od.ShowDialog() == DialogResult.OK) { [COLOR=Green] /// we need to get the short path name of our file /// eg: E:\Iron Maiden\(1980) Iron Maiden\04 - Phantom Of The Opera.mp3 /// becomes E:\IRONMA~1\(1980)~1\04-PHA~1.MP3 /// to do this we use the GetShortPathName API /// and the System.Text.StringBuilder class to receive the ShortPathName. [/COLOR] StringBuilder sbuild = [COLOR=Blue]new[/COLOR] StringBuilder(256); GetShortPathName(od.FileName , sbuild , sbuild.Capacity); Play(sbuild.ToString()); } } [COLOR=Blue]private void[/COLOR] Play([COLOR=Blue]string[/COLOR] Mp3) { [COLOR=Green]/// open the mp3 file ( mp3 uses the Type MPEGVideo ) [/COLOR] [COLOR=Blue]int[/COLOR] x = mciSendString("open " + Mp3 + " Type MPEGVideo Alias " + Alias , [COLOR=Blue]null[/COLOR] , 0 , 0); [COLOR=Green]/// play the mp3 [/COLOR] x = mciSendString("play " + Alias , [COLOR=Blue]null[/COLOR] , 0 , 0); } [COLOR=Blue]private void[/COLOR] Stop() { [COLOR=Blue]int[/COLOR] x = mciSendString("stop " + Alias , [COLOR=Blue]null[/COLOR] , 0 , 0); } [COLOR=Blue]private void[/COLOR] btnstop_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e) { Stop(); }
Thanks dynamic_sysop if I have some extra time I'll try that one.
Nice to see you back. :)
I already tested it. Yeah it works but in only one file. :(
Actually I add all the list of mp3 in a listbox and select a file then play it and it works fine but when I choose another mp3 file in the listbox then the problem occur, it wont work. :(
sorry i haven't been able to post a message back, my wife was rushed in to hospital yesterday, she has severe pneumonia ( which isn't good considering she's also 33 weeks pregnant ) :cry:
if i get an opertunaty i'll do a Directsound example sometime, but it'll be a while.
regards, den ( aka dynamic )
I'm very sorry to hear that. I hope your wife will get well considering she's bearing your 3rd child and it's a boy(in your sig).Quote:
Originally Posted by dynamic_sysop
Don't worry mate I'll just wait till you reply. :)
3rd child? :eek: it's our 5th child ( my 6th child ) mate :afrog:
no wonder i'm starting to go grey :sick:
using DirectX audio is easy too
just add a reference to DirectX.AudioVideoPlayback;
then:
it maybe .Start() instead of Play but i cant rememberCode:Audio myAudioFile = new Audio("Pathtoaudiofile.xxx");
myAudioFile.Play();
pretty much thats it :)
Using directx to play will only work if directx is installed right? :)
right :)
but directx is built into Windows, since Windows 98SE (I think) but defo in 2000/2003/Longhorn/Windows 98SE/ME
Roger that..:)