|
-
Aug 17th, 2005, 08:16 PM
#1
Thread Starter
Lively Member
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?
-
Aug 17th, 2005, 10:04 PM
#2
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).
-
Aug 17th, 2005, 10:54 PM
#3
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!!
-
Aug 17th, 2005, 10:59 PM
#4
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.
-
Aug 18th, 2005, 03:23 AM
#5
Thread Starter
Lively Member
Re: need advice on playing audio file
how do i add the a Windows Media Player ActiveX control into the form?
-
Aug 18th, 2005, 03:52 AM
#6
Thread Starter
Lively Member
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?
-
Aug 18th, 2005, 03:57 AM
#7
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.
-
Aug 18th, 2005, 04:15 AM
#8
Thread Starter
Lively Member
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.
-
Aug 18th, 2005, 04:23 AM
#9
Re: need advice on playing audio file
The Ctlcontrol property contains those methods.
-
Aug 18th, 2005, 09:10 AM
#10
Thread Starter
Lively Member
Re: need advice on playing audio file
where can i find this Ctlcontrol ?
-
Aug 18th, 2005, 06:18 PM
#11
Re: need advice on playing audio file
 Originally Posted by jmcilhinney
The Ctlcontrol property contains those methods.
Sorry, actually "Ctlcontrols", but Intellisense would have told you that, e.g.
VB Code:
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
-
Aug 18th, 2005, 08:10 PM
#12
Thread Starter
Lively Member
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:
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
Last edited by sanko84; Aug 18th, 2005 at 08:16 PM.
-
Aug 18th, 2005, 08:38 PM
#13
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.
-
Aug 18th, 2005, 09:48 PM
#14
Fanatic Member
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:
Dim md As New WMPLib.WindowsMediaPlayer
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! 
-
Aug 18th, 2005, 09:50 PM
#15
Member
Re: need advice on playing audio file
is it possible to call the window media player from the computer by using visual basic.net???
-
Aug 18th, 2005, 10:00 PM
#16
Fanatic Member
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:
Dim md As New WMPLib.WindowsMediaPlayer
md.openPlayer("C:\1.mp3")
Godwin
Help someone else with what someone helped you! 
-
Aug 18th, 2005, 10:02 PM
#17
Re: need advice on playing audio file
 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:
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:
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.
-
Aug 18th, 2005, 10:04 PM
#18
Re: need advice on playing audio file
 Originally Posted by uniquegodwin
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")
Are you saying that using this method your object is controllable by Ctlcontrols but appears as its own window?
-
Aug 18th, 2005, 10:07 PM
#19
Fanatic Member
-
Aug 18th, 2005, 10:13 PM
#20
Fanatic Member
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:
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
Godwin
Help someone else with what someone helped you! 
-
Aug 18th, 2005, 10:21 PM
#21
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.
-
Aug 18th, 2005, 10:32 PM
#22
Fanatic Member
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:
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
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! 
-
Aug 18th, 2005, 11:08 PM
#23
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.
-
Aug 19th, 2005, 02:54 AM
#24
Fanatic Member
-
Aug 19th, 2005, 03:07 AM
#25
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.
-
Aug 19th, 2005, 03:21 AM
#26
Fanatic Member
-
Jan 13th, 2008, 01:33 AM
#27
Addicted Member
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
-
Jan 13th, 2008, 03:00 AM
#28
Re: need advice on playing audio file
 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.
-
Jan 13th, 2008, 05:58 AM
#29
-
Jan 13th, 2008, 06:51 AM
#30
Addicted Member
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
-
Jan 13th, 2008, 07:07 AM
#31
Addicted Member
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 ?
-
Jan 13th, 2008, 09:18 AM
#32
Addicted Member
Re: need advice on playing audio file
no, i want to play it with my vb application not the media player
-
Jan 13th, 2008, 10:45 AM
#33
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
-
Jan 14th, 2008, 01:12 AM
#34
Addicted Member
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.?
-
Jan 14th, 2008, 01:23 AM
#35
Re: need advice on playing audio file
 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.
-
Jan 14th, 2008, 02:07 AM
#36
Addicted Member
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?
-
Jan 14th, 2008, 11:03 AM
#37
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|