I’m having a weird problem when playing a wav file (0.034 seconds long) embedded as a resource on my VB.NET application.
My application is supposed to loop the sound asynchronously while the button is pressed and stop the sound when the button is released.
The problem is that there seems to be a delay when the button is pressed and released quickly, especially if it is clicked multiple times in a row.
This delay makes the audio sound choppy and it also hangs the application until the queue of calls to play() and stop() ends.
However, I noticed that if I have an audio player running (playing a sound file or paused, not in stop) simultaneously when running my application, the delay or latency is gone.
The players I have tested have been: Windows Media Player, Windows Media Player Classic – Home Cinema, Foobar2000 and QuickTime.
Here’s the code:
Option Explicit On
Option Strict On
Public Class frmMain
Private btnPlayPressed As Boolean
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
btnPlayPressed = False
End Sub
Private Sub btnPlay_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnPlay.MouseDown
If Not btnPlayPressed Then
My.Computer.Audio.Play(My.Resources.Beep, AudioPlayMode.BackgroundLoop)
btnPlayPressed = True
End If
End Sub
Private Sub btnPlay_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnPlay.MouseUp
If btnPlayPressed Then
My.Computer.Audio.Stop()
btnPlayPressed = False
End If
End Sub
Private Sub btnPlay_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnPlay.KeyDown
If e.KeyCode.Equals(Keys.Space) Then
btnPlay_MouseDown(sender, Nothing)
End If
End Sub
Private Sub btnPlay_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btnPlay.KeyUp
If e.KeyCode.Equals(Keys.Space) Then
btnPlay_MouseUp(sender, Nothing)
End If
End Sub
End Class
I have also used various of the other audio functions available in VB.NET and they all have this problem but I’m sticking with My.Computer.Audio because of it’s simplicity and readability.
Why does having audio players running remove the delay of My.Computer.Audio.play?
Is there a way to remove the delay from within my code?
Other details:
IDE: Visual Studio 2010 Express
OS: Vista sp2 x86/ Windows 7 x64
There could be a couple reasons for this. The first might be that other software is specifically lowering the buffer size for playback. When this is done, there is lower delay.
The second reason might have to do with the sampling rate. If there is a buffer size set in bytes and the sample rate doubles, then that buffer lasts half as long. If you are playing audio in your application at a certain rate, and other software plays at a different rate, then perhaps Windows’ audio mixing service adjusts the buffer differently.
There is no way to remote delay completely. That’s not how digital audio works. However, you can reduce it significantly by using ASIO or similar.