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?
Printable View
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?
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).
are those "MCI" functions supposed to work on any XP+ machine?
I really don't know much about MCI, just that it is used for audio and video stuff.
how do i add the a Windows Media Player ActiveX control into the form?
how about when i want to play the mp3, i just call the windows media player out from my computer?
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.
i have added the WMP ActiveX control but how do i start by giving them the function like play, stop or pause.
The Ctlcontrol property contains those methods.
where can i find this Ctlcontrol ?
Sorry, actually "Ctlcontrols", but Intellisense would have told you that, e.g.Quote:
Originally Posted by jmcilhinney
VB Code:
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
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:
Private Sub AxWindowsMediaPlayer1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxWindowsMediaPlayer1.Enter Me.AxWindowsMediaPlayer1.Ctlcontrols.play() Me.AxWindowsMediaPlayer1.Ctlcontrols.stop() Me.AxWindowsMediaPlayer1.Ctlcontrols.pause() End Sub
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.
Hi..:)
Just add reference to wmp.dll from the com components...
Then,maybe this is what your looking for...
Use the md.controls.play ,md.controls.pause to just control your song :)VB Code:
Dim md As New WMPLib.WindowsMediaPlayer md.URL = ("c:\something.mp3")
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
is it possible to call the window media player from the computer by using visual basic.net???
irrrrr...u couldve asked this on a seperate thread...
anyways..
heres how u do that too
VB Code:
Dim md As New WMPLib.WindowsMediaPlayer md.openPlayer("C:\1.mp3")
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:Quote:
Originally Posted by longdexin
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:
Process.Start(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.VB Code:
Process.Start("wmplayer", """" & IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), "MyMusicFile.mp3") & """")
Are you saying that using this method your object is controllable by Ctlcontrols but appears as its own window?Quote:
Originally Posted by uniquegodwin
Hi Jm :)
yep,It opens windows media player up playing that file :)
Jm,I think I misunderstood your question...
I think you mean using like this...i just tried it..
VB Code:
Dim md As New WMPLib.WindowsMediaPlayer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load md.openPlayer("C:\1.mp3") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click md.controls.stop() md.controls.pause() End Sub
This doesnt work for me ..maybe there is a way to control from the form..im not sure :)
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.
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.
But,it gives me an error saying invalid cast..VB Code:
Dim md As New WMPLib.WindowsMediaPlayer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Controls.Add(md) md.stretchToFit = True End Sub
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.
oh,I misunderstood the interface for an activex control i think
Ill just check what an interface is then :)
Thanks :)
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.
ohh,I got it now :)
Thanks Jm for your time and explaination :)
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.Quote:
Originally Posted by SidCMC
Thank You Jm.
I'll surely ask you if I have doubts.
Thanks again.:) :wave:
is the information provided here still up-to-date? or is there something new in Visual Studio 2008 that can help play *.mp3?
thx
Well Perito,
I guess the best thing would
be to call the media player application
directly using application.start() method.
What do you think ?:)
no, i want to play it with my vb application not the media player
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
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.Quote:
Originally Posted by SidCMC
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?
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.
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.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