Hello, I am trying to figure out how to add sounds to a form...I've searched the forums and found nothing really. I have VB.Net 2003...dunno if that matters to you . I also would like to know how to add in a video file if it's possible...
I just really need an example of the code, it would be appreciated.
Public Declare Function playa Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Sub PlayWav(sFile As String)
If Dir(sFile$) <> "" Then Call playa(sFile, SND_FLAG)
End Sub
.NET 1.x has no real support for sound. You can use the PlaySound API yourself, examples of which are plentiful on the forum, or you can use a third-party wrapper class. Follow the Mentalis link in my signature and you'll find just such a class.
As for video, again it helps to be a little more specific. There are exactly 2.75 squintillion different video formats for PC. Do you want to display a Flash file, an AVI, some other specific format or many different formats? There won't be any one way to handle every format but there are media players that can handle most of the common ones. Unless you want to do a lot of work yourself you'll want to add a third party component to your form to handle the playback which you can than control in code or provide UI controls for the user. The easiest option is Windows Media Player because it is alreday present on every Windows system. You can add the WMP ActiveX control to your Toolbox (right-click, Add/Remove) from the COM Components tab. You can then add an instance to your form like any other control. You should then read the documentation on MSDN, use Intellisense to see what members it has and search the forum to see what issues others have had and how they were solved.
Not to say that you necessarily should ditch VS.NET 2003 if you paid for it and it does what you want but you can get VB 2005 Express for free. It's a fairly powerful IDE, although it does lack some of the features of its big brothers. It will upgrade your existing projects to the new format and many won't erquire any changes at all, though some probably will require minor changes. It's not a bad idea to give it a try but note that the project upgrade process is not reversible so make sure you keep a backup copy of your project if you're going to want to open it in VS.NET 2003 again. This also means that any changes you make in 2005 cannot be transferred back to 2003.
Note that the upgrade process being irreversible means that the IDE will not do it for you. You can actually edit some text files manually and then open your project in 2003 again, but there's every chance that any code you write in 2005 will not be compatible because there have been language and class library changes.
There's a VB.NET Games Programming tutorial link in my signature. It has some information on collision detection but I'm not sure what. Having said that, it's relatively easy with two controls:
VB Code:
Private Function DetectCollision(ByVal ctl1 As Control, ByVal ctl2 As Control) As Boolean
Return ctl1.Bounds.IntersectsWith(ctl2.Bounds)
End Function
Have a good look at the attched image though, to see exactly what is detected. If you want both cases to be detected as a collision then you'll need to create a new Rectangle for one of the controls that is one pixel larger in all directions.
Well, collision detection is working now. Thanks for all your help guys, though sounds aren't working for 2005...I'm trying this:
VB Code:
My.Computer.Audio.Play("intro.wav")
Its in the bin folder...it doesnt work when run inside and outside (built) .Net. It gives this error: "Sound API only supports playing PCM wave files.".
Last edited by Aloshi; Oct 18th, 2006 at 03:09 PM.
Dim mediaSoundPlayer As New System.Media.SoundPlayer
If openFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
mediaSoundPlayer.SoundLocation = openFile.FileName
mediaSoundPlayer.Play()
End If
mediaSoundPlayer.Play()
Presumably your file is not a PCM wave file, whatever exactly that is. What happens if you double-click that file in Windows Explorer? Does it play properly? If so then I guess Windows has support for different wave formats but the API used by that method doesn't. Perhaps you should Google to find out exactly what a PCM wave file is and if and how it differs from any other type of wave file.
Edit: Just took my own advice and found that wave files normally store data in the uncompressed PCM format but can contain compressed audio too. That I didn't know, but it suggests that perhaps your file is compressed and Windows, or at least the API being used by My.Computer.Audio.Play, supports only the uncompressed PCM format.
Last edited by jmcilhinney; Oct 18th, 2006 at 05:33 PM.