Results 1 to 37 of 37

Thread: need advice on playing audio file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    88

    need advice on playing audio file

    i need to know what should i import so that i can play the audio file like eg. mp3, wav. and what method should i see in?

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

    Re: need advice on playing audio file

    You can play WAV files using the PlaySound API function. It has been mentioned numerous times so a forum search should give you plenty of matches. For MP3s you will need to either add a Windows Media Player ActiveX control to your form or use MCI (Media Control Interface).
    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
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: need advice on playing audio file

    are those "MCI" functions supposed to work on any XP+ machine?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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

    Re: need advice on playing audio file

    I really don't know much about MCI, just that it is used for audio and video stuff.
    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

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    88

    Re: need advice on playing audio file

    how do i add the a Windows Media Player ActiveX control into the form?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    88

    Re: need advice on playing audio file

    how about when i want to play the mp3, i just call the windows media player out from my computer?

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

    Re: need advice on playing audio file

    You can add the WMP ActiveX control to the IDE Toolbox from the COM Components tab (right-click and select Add/Remove). Then you just add an instance of the control to your form like any other control.
    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    88

    Re: need advice on playing audio file

    i have added the WMP ActiveX control but how do i start by giving them the function like play, stop or pause.

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

    Re: need advice on playing audio file

    The Ctlcontrol property contains those methods.
    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

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    88

    Re: need advice on playing audio file

    where can i find this Ctlcontrol ?

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

    Re: need advice on playing audio file

    Quote Originally Posted by jmcilhinney
    The Ctlcontrol property contains those methods.
    Sorry, actually "Ctlcontrols", but Intellisense would have told you that, e.g.
    VB Code:
    1. Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
    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

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    88

    Re: need advice on playing audio file

    hmm ok, i put those into the form. But right now how am i going take the mp3 file and play it? And is this the right way to put the these property?




    VB Code:
    1. Private Sub AxWindowsMediaPlayer1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxWindowsMediaPlayer1.Enter
    2.         Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
    3.         Me.AxWindowsMediaPlayer1.Ctlcontrols.stop()
    4.         Me.AxWindowsMediaPlayer1.Ctlcontrols.pause()
    5.     End Sub
    Last edited by sanko84; Aug 18th, 2005 at 08:16 PM.

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

    Re: need advice on playing audio file

    Keep in mind that I have never used the WMP control so I am just working this stuff out by reading, using trial and error and a bit of intuition, the same as you could yourself.

    Ctlcontrols is a property of type WMPLib.IWMPControls, and play(), stop() and pause() are methods of that property. If you want to use code to control playback then those methods do just what the names suggest. To play a media file you assign its location to the URL property, which started playback immediately when I tried it. All the normal UI controls are available to the user, so unless you want to be controlling playback in code you don't need to use play, stop, pause, etc. because the user can just press the appropriate button on the control.

    I suggest you do some reading about the WMP control on MSDN to gain an understanding of it. Here's a good place to start.
    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

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

    Re: need advice on playing audio file

    Hi..
    Just add reference to wmp.dll from the com components...
    Then,maybe this is what your looking for...
    VB Code:
    1. Dim md As New WMPLib.WindowsMediaPlayer
    2.         md.URL = ("c:\something.mp3")
    Use the md.controls.play ,md.controls.pause to just control your song
    You can just try out all the functions available in the md.blah blah...
    You can also declare it as windowsmediaplayerclass.I am not sure what is the difference between declaring as windowsmediaplayer and windowsmediaplayerclass.Both sound same to me though
    Godwin

    Help someone else with what someone helped you!

  15. #15
    Member
    Join Date
    Jun 2005
    Location
    Singapore
    Posts
    55

    Re: need advice on playing audio file

    is it possible to call the window media player from the computer by using visual basic.net???

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

    Re: need advice on playing audio file

    irrrrr...u couldve asked this on a seperate thread...
    anyways..
    heres how u do that too
    VB Code:
    1. Dim md As New WMPLib.WindowsMediaPlayer
    2.         md.openPlayer("C:\1.mp3")
    Godwin

    Help someone else with what someone helped you!

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

    Re: need advice on playing audio file

    Quote Originally Posted by longdexin
    is it possible to call the window media player from the computer by using visual basic.net???
    You would use Process.Start, like starting any other external process. If WMP is the default app for an MP3 file, you can just use something like this:
    VB Code:
    1. Process.Start(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), "MyMusicFile.mp3"))
    If you want to make sure you use WMP whether it is the default player or not you would need to use something like this:
    VB Code:
    1. Process.Start("wmplayer", """" & IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), "MyMusicFile.mp3") & """")
    Note that you need to put the extra double quotes around the second argument to account for spaces in the file path.
    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

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

    Re: need advice on playing audio file

    Quote Originally Posted by uniquegodwin
    irrrrr...u couldve asked this on a seperate thread...
    anyways..
    heres how u do that too
    VB Code:
    1. Dim md As New WMPLib.WindowsMediaPlayer
    2.         md.openPlayer("C:\1.mp3")
    Are you saying that using this method your object is controllable by Ctlcontrols but appears as its own window?
    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

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

    Re: need advice on playing audio file

    Hi Jm
    yep,It opens windows media player up playing that file
    Godwin

    Help someone else with what someone helped you!

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

    Re: need advice on playing audio file

    Jm,I think I misunderstood your question...
    I think you mean using like this...i just tried it..
    VB Code:
    1. Dim md As New WMPLib.WindowsMediaPlayer
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         md.openPlayer("C:\1.mp3")
    4.     End Sub
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         md.controls.stop()
    8.         md.controls.pause()
    9.     End Sub

    This doesnt work for me ..maybe there is a way to control from the form..im not sure
    Godwin

    Help someone else with what someone helped you!

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

    Re: need advice on playing audio file

    I broke my own rule and asked a question that I could just test myself, which I have now done.

    I just did a bit of testing and this is what I believe to be the case:

    If you want to display a WMP control on your form, you need to add an instance of the AxWindowsMediaPlayer class to your form. This allows the user to control playback via the UI and the app to control playback via the Ctlcontrols property.

    If you want to provide audio without an interface you should create an instance of the WindowsMediaPlayerClass class. Note that WindowsMediaPlayer is an Interface, while WindowsMediaPlayerClass is a class. I'm guessing that the WindowsMediaPlayerClass class implements the WindowsMediaPlayer interface. Unless for the purposes of polymorphism, you should not really be working with instances of an interface, so I'd say go with the class. The WindowsMediaPlayerClass class has methods named play, stop, pause, etc.

    When you call openPlayer on a WindowsMediaPlayerClass object, it opens an instance of WMP in its own window, but you lose the ability to control it through code. After calling openPlayer I found that play, pause, stop, and close had no effect.
    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

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

    Re: need advice on playing audio file

    yeah they have no effect
    I understand the difference between windowsmediaplayer and windowsmediaplayerclass now.
    But then even for windowsmediaplayer,Im not able to get it displayed on the form.There seems to be no show or visible property.Its invisible.
    I even tried this.
    VB Code:
    1. Dim md As New WMPLib.WindowsMediaPlayer
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Me.Controls.Add(md)
    4.         md.stretchToFit = True
    5.     End Sub
    But,it gives me an error saying invalid cast..
    Last edited by uniquegodwin; Aug 18th, 2005 at 10:43 PM.
    Godwin

    Help someone else with what someone helped you!

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

    Re: need advice on playing audio file

    Like I said, WindowsMediaPlayerClass is for playing audio files without an interface. It is not a control so cannot be added to your form's Controls collection. If you want an interface on your form, you can either create your own and use WindowsMediaPlayerClass, or you can add an instance of the AxWindowsMediaPlayer class to your form, which is an ActiveX control that encapsulates the functionality of the WindowsMediaPlayerClass class and provides an interace basically identical to the main player window of WMP itself.
    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

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

    Re: need advice on playing audio file

    oh,I misunderstood the interface for an activex control i think
    Ill just check what an interface is then

    Thanks
    Godwin

    Help someone else with what someone helped you!

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

    Re: need advice on playing audio file

    Bad choice of words I think. What I meant was that WindowsMediaPlayerClass is not a control so it has no user interface, i.e. it cannot be visible on a form. If you want a user interface you need to use an AxWindowsMediaPlayer object, which is an ActiveX control and therefore can be visible on a form. When I said that WMPLib.WindowsMediaPlayerClass is a class and WMPLib.WindowsMediaPlayer is an interface, I meant a programming interface that a class can implement, like IEnumerable or ISerializable. Notice the symbols in the Intellisense list for WindowsMediaPlayer and WindowsMediaPlayerClass, which indicate what type of object they are.
    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

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

    Re: need advice on playing audio file

    ohh,I got it now

    Thanks Jm for your time and explaination
    Godwin

    Help someone else with what someone helped you!

  27. #27
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Re: need advice on playing audio file

    Dear Jmcilhinney,
    IWMPControls is an interface and the property Ctlcontrols returns
    an object of this interface.
    So the methods play(), pause() etc are the methods of the interface
    and NOT the property...A property never contains methods.
    Get your facts right.
    Sid

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

    Re: need advice on playing audio file

    Quote Originally Posted by SidCMC
    Dear Jmcilhinney,
    IWMPControls is an interface and the property Ctlcontrols returns
    an object of this interface.
    So the methods play(), pause() etc are the methods of the interface
    and NOT the property...A property never contains methods.
    Get your facts right.
    Sid
    Thank you for such a positive input to a thread that was resolved over two years ago with your very first post. I know that a property doesn't have methods itself. I was simplifying the fact that the object referred to by that property had those methods. If you'd like any clarifications on any other glaringly obvious simplifications I may have posted years ago then by all means ask me.
    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

  29. #29
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Re: need advice on playing audio file

    Thank You Jm.
    I'll surely ask you if I have doubts.
    Thanks again.

  30. #30
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: need advice on playing audio file

    is the information provided here still up-to-date? or is there something new in Visual Studio 2008 that can help play *.mp3?
    thx

  31. #31
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Re: need advice on playing audio file

    Well Perito,
    I guess the best thing would
    be to call the media player application
    directly using application.start() method.
    What do you think ?

  32. #32
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: need advice on playing audio file

    no, i want to play it with my vb application not the media player

  33. #33
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: need advice on playing audio file

    DirectX is very flexible in playing many sound files but it requires the SDK - just more hassle.

    Another way is to use MCI as already suggested here so long ago. It works great and never failed me before, even on computers without media player.
    Code:
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
        (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
        ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
     
    'start playback
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click  
    
            Dim fileName As String
            fileName = "C:\test.mp3"
    
            'open the file, the alias part is how you identify the song in your code
            mciSendString("open " & fileName & " type mpegvideo alias myMP3", String.Empty, 0, 0)
    
            'start playing
            mciSendString("play myMP3", String.Empty, 0, 0)        
    
        End Sub
    
    
    'stop playback
        Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim fileName As String
            fileName = "C:\test.mp3"
    
            mciSendString("open " & fileName & " type mpegvideo alias myMP3", String.Empty, 0, 0)
    
            'stop playing
            mciSendString("stop myMP3", String.Empty, 0, 0)
    
            'close the file
            mciSendString("close myMP3", String.Empty, 0, 0)
    
        End Sub
    Last edited by Half; Jan 13th, 2008 at 10:52 AM.
    VB 2005, Win Xp Pro sp2

  34. #34
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Re: need advice on playing audio file

    Hi half,
    i am not able to add winmm.dll as a reference in my project.
    Is it compulsory to use API calls.??
    Is there any other way out.?

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

    Re: need advice on playing audio file

    Quote Originally Posted by SidCMC
    Hi half,
    i am not able to add winmm.dll as a reference in my project.
    Is it compulsory to use API calls.??
    Is there any other way out.?
    Winmm.dll is part of the Windows API. You cannot reference it from a .NET project as it's neither a .NET assembly or a COM component. To access the method sit exports you must use platform invoke as shown.
    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

  36. #36
    Addicted Member SidCMC's Avatar
    Join Date
    Jan 2008
    Location
    Mumbai, India
    Posts
    166

    Re: need advice on playing audio file

    Thanks Jm..but when I use the MCI given above in the form_load event..for testing purposes , the form displays itself and the .mp3 doesnt play at all.
    What could be the possible reason?

  37. #37
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: need advice on playing audio file

    The following code works for me. Note that you must close the mp3 file when exiting the application or you'll get errors. Also it may sound trivial but make sure the file path of the mp3 is correct.
    Code:
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
        (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
        ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    
    
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
    
            Dim fileName As String
            fileName = "C:\test.mp3"
    
            'open the file, the alias part is how you identify the song in your code
            mciSendString("open " & fileName & " type mpegvideo alias myMP3", String.Empty, 0, 0)
    
            'start playing
            mciSendString("play myMP3", String.Empty, 0, 0)
        End Sub
    
    
    
        Private Sub Form1_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    
            'close the file / stop playback
            mciSendString("close myMP3", String.Empty, 0, 0)
        End Sub
    As part of my personal opinion, I like API calls very much and prefer to use them any time when there isn't any native .net solution. MCI also lets you play more than one music file simultaneously.
    VB 2005, Win Xp Pro sp2

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