Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > How can I check if a wav file is playing?
 

How can I check if a wav file is playing?

Hello. I am using VS 2005 Professional and writing a guitar tuner app with visual basic. I have a 6 wav files that play in button click events using the My.Computer.Audio.Play() method. I need to animate a picture box meter while a wav file is playing. I have searched in the library, but I can't find any relevant info for checking if a sound is playing for vb. Is there a way I can check to see if the wav file is playing?
BrianMcCumber  Tuesday, November 24, 2009 4:52 PM
Well I found out why it was throwing an error. I had to Imports Microsoft.DirectX. I added that line in the namespace, and now I plays the wav just fine, with no errors. Using the direct sound isn't as hard as I thought it would be.
BrianMcCumber  Sunday, November 29, 2009 6:43 PM
Probably, but...

...I have given this particular application some thought, and you should really bite the bullet and use DirectX audio (DirectSound API) for this. For one thing, you will be able to control the buffers that are playing in real time, and the same sort of availability is there for the wave in.

Calculating the frequency of the input wave is not difficult, but in using the Multimedia API, you will be using files, which will make your application very clunky.
jinzai  Tuesday, November 24, 2009 6:45 PM
I was considering that, and trying to find some sample code to setting up a direct sound buffer correctly, but it was all written in c++ orc#. I have the direct x sdk installed, with sample codes but the managed direct xsamples are all c#. I wouldn't mind using c#, but I am not that familiar with it, and I don't know how to use a c# file in my vb programs, yet. Can anyone point me in the right direction? Thanks for answering my post.
BrianMcCumber  Tuesday, November 24, 2009 10:32 PM
Well I have managed to get a wav file to play using direct sound, but only after the button click throws an exception:


Managed Debugging Assistant 'LoaderLock' has detected a problem in 'C:\Users\Seth\Documents\Visual Studio 2005\Projects \VB Solutions\Direct Sound Test\Direct Sound Test\bin\Debug\Direct Sound Test.vshost.exe'. Additional Information: DLL 'C:\Windows\assembly\GAC\Microsoft.DirectX\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.


After I click continue on the exception, the wav plays, and plays every time I click the button. Below this is the code I am using. Any help with this would be appreciated.

Imports Microsoft.DirectX.DirectSound

Public Class Form1

    Private Sub xPlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xPlayButton.Click

        Dim sDevice As New Device
        sDevice.SetCooperativeLevel(Me, CooperativeLevel.Normal)
        Dim secBuffer As New SecondaryBuffer(My.Resources.b, My.Resources.b.Length, sDevice)

        secBuffer.Play(0, BufferPlayFlags.Default)

    End Sub

End Class
BrianMcCumber  Wednesday, November 25, 2009 3:52 PM
I'm on vacation, but...it seems like the buffer is not completely setup. DirectSound is a little cantankerous, iirc. (Its been 8 years since I've worked with it, and that was in C++.)

I am going to work up a little bit of code to FFT the microphone input...which is how I want to implement a tuner. I will begin by working up something more along the lines of your concept. I will see what you need to do in order to get what you have already done working...I'm sure you are very close already.)

Generating the notes "on the fly" is pretty simple, really. It is all about the sin at a certain point in time...the generation is easily accomplished with a for/next loop. The file format is more hassle than the audio generation.
jinzai  Saturday, November 28, 2009 2:19 AM
I willappreciate any help on this matter. By the way, I am using VS 2005 pro, and my app is written in VB 2005. I have 6 wav files that are the correct tuning for the guitar strings, open E tuning. I have made a custom form with a dial that moves when you click a note button, what I need is a way to load the wav file into a buffer on a button click. Then while it is playing, I will use the buffer.getPosition method in a timer set at around 250 to 500 milliseconds, and if the wav is still playing, I will animate a meter that is a picture box within the timer tick event. Everything is finished, except the meter animation.

I was thinking a simple class for direct sound that constructs/destructs a new direct sound device/buffer with the play, stop and getPosition methods of direct sound exposed within the class would be nice. Plus, I could reuse the class in other applications. If I could get the device/buffer constructor worked out, writing the class would be a fairly simple process. Then the class could contain a playWav, stopWav, and getWavPosition method. Then you could call:

Dim myNewWav as New Audio
myNewWav = makeWavBuffer(fileName)
playWav(myNewWav)
stopWav(myNewWav)
Dim myWavPostion as Integer
myWavPosition = cInt(getWavPostion(myNewWav))

Well, thats just a rough example of what I'd like to do, I am still pretty new to classes. But I hope that it will give you a better idea of what I am trying to do. I will help with what I can. Thanks alot for reading my post and responding.
BrianMcCumber  Saturday, November 28, 2009 1:44 PM
Well I found out why it was throwing an error. I had to Imports Microsoft.DirectX. I added that line in the namespace, and now I plays the wav just fine, with no errors. Using the direct sound isn't as hard as I thought it would be.
BrianMcCumber  Sunday, November 29, 2009 6:43 PM

You can use google to search for other answers

Custom Search

More Threads

• dbtype of common.dbparameter
• Two Classes with the same Data Structure.. saving code? Inheriting a structure?
• Multiply Page Printing HasMorePages
• Problems with streamread
• Automatically activated form event
• System.InvalidProgramException was unhandled ???
• can colordialog show only named colors
• other languages
• Calling a function from a seperate form
• Searching for a string in a string array