Results 1 to 14 of 14

Thread: play mp3 file

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    play mp3 file

    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.

    Btw Im using 1.0 framework.
    Last edited by mar_zim; Aug 31st, 2005 at 10:17 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: play mp3 file

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: play mp3 file

    got any good article/tutorial about that?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: play mp3 file

    Only the MSDN topics I'm afraid as I haven't really used either myself.

    MCI
    WMP

    If you're interested in MCI then you could check out some of wldrumstcs recent posts, as he's building a media player using purely MCI.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: play mp3 file

    top of your code window ...
    VB Code:
    1. [COLOR=Green]/// add these 2 references ... [/COLOR]
    2. [COLOR=Blue]using[/COLOR] System.Runtime.InteropServices;
    3. [COLOR=Blue]using[/COLOR] System.Text;
    to use, a simple bit of code that allows you to open / play / stop an mp3 i put together ...
    VB Code:
    1. [DllImport("winmm.dll", EntryPoint="mciSendStringA")]
    2.         [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);
    3.         [DllImport("kernel32.dll", EntryPoint="GetShortPathNameA")]
    4.         [COLOR=Blue]private static extern int[/COLOR] GetShortPathName ([COLOR=Blue]string[/COLOR] lpszLongPath, StringBuilder lpszShortPath, [COLOR=Blue]int[/COLOR] cchBuffer);
    5.  
    6.         [COLOR=Blue]private string[/COLOR] Alias = "MP3";
    7.  
    8.         [COLOR=Blue]private void[/COLOR] btnplay_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
    9.         {
    10.             OpenFileDialog od = [COLOR=Blue]new[/COLOR] OpenFileDialog();
    11.             od.Filter = "MP3 Audio(*.mp3)|*.mp3";
    12.             od.RestoreDirectory = [COLOR=Blue]false[/COLOR];
    13.             [COLOR=Blue]if[/COLOR](od.ShowDialog() == DialogResult.OK)
    14.             {
    15.                [COLOR=Green] /// we need to get the short path name of our file
    16.                 /// eg: E:\Iron Maiden\(1980) Iron Maiden\04 - Phantom Of The Opera.mp3
    17.                 /// becomes  E:\IRONMA~1\(1980)~1\04-PHA~1.MP3
    18.                 /// to do this we use the GetShortPathName API
    19.                 /// and the System.Text.StringBuilder class to receive the ShortPathName. [/COLOR]                
    20.                 StringBuilder sbuild = [COLOR=Blue]new[/COLOR] StringBuilder(256);
    21.                 GetShortPathName(od.FileName , sbuild , sbuild.Capacity);
    22.  
    23.                 Play(sbuild.ToString());
    24.             }
    25.         }
    26.  
    27.         [COLOR=Blue]private void[/COLOR] Play([COLOR=Blue]string[/COLOR] Mp3)
    28.         {
    29.             [COLOR=Green]/// open the mp3 file ( mp3 uses the Type MPEGVideo ) [/COLOR]
    30.             [COLOR=Blue]int[/COLOR] x = mciSendString("open " + Mp3 + " Type MPEGVideo Alias " + Alias , [COLOR=Blue]null[/COLOR] , 0 , 0);
    31.             [COLOR=Green]/// play the mp3 [/COLOR]
    32.             x = mciSendString("play " + Alias , [COLOR=Blue]null[/COLOR] , 0 , 0);
    33.         }
    34.  
    35.         [COLOR=Blue]private void[/COLOR] Stop()
    36.         {
    37.             [COLOR=Blue]int[/COLOR] x = mciSendString("stop " + Alias , [COLOR=Blue]null[/COLOR] , 0 , 0);
    38.         }
    39.  
    40.         [COLOR=Blue]private void[/COLOR] btnstop_Click([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
    41.         {
    42.             Stop();
    43.         }
    hope it helps
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: play mp3 file

    Thanks dynamic_sysop if I have some extra time I'll try that one.

    Nice to see you back.

  7. #7

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: play mp3 file

    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.

  8. #8
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: play mp3 file

    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 )

    if i get an opertunaty i'll do a Directsound example sometime, but it'll be a while.

    regards, den ( aka dynamic )
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  9. #9

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: play mp3 file

    Quote Originally Posted by dynamic_sysop
    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 )

    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).

    Don't worry mate I'll just wait till you reply.

  10. #10
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: play mp3 file

    3rd child? it's our 5th child ( my 6th child ) mate
    no wonder i'm starting to go grey
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  11. #11
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: play mp3 file

    using DirectX audio is easy too

    just add a reference to DirectX.AudioVideoPlayback;

    then:

    Code:
    Audio myAudioFile = new Audio("Pathtoaudiofile.xxx");
    
    myAudioFile.Play();
    it maybe .Start() instead of Play but i cant remember
    pretty much thats it

  12. #12
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: play mp3 file

    Using directx to play will only work if directx is installed right?
    Godwin

    Help someone else with what someone helped you!

  13. #13
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: play mp3 file

    right
    but directx is built into Windows, since Windows 98SE (I think) but defo in 2000/2003/Longhorn/Windows 98SE/ME

  14. #14
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: play mp3 file

    Roger that..
    Godwin

    Help someone else with what someone helped you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width