S
Shashank V Ray
Guest
Hello community,
I'm stuck at a position where I'm creating a VB .NET standalone application using VS 2017.
I've made a little progress in playing 2 audio files on clicking a button among the several others without stopping the previously playing WAV audio file.
But now I want to achieve a something where a single button should play an audio file (which I now have already come up with) but on clicking the same button on the application, the sound that it was playing by the click of the same old button should not be interrupted.
I'm completely a beginner to .NET and still have come up with this code so far. Help me with this new problem of mine. Ignore the comments.
Imports System.Runtime.InteropServices
Public Class Form1
Private Wave1 As New NAudio.Wave.WaveOut
Private Snds As New MultiSounds
Private WithEvents ButtonA As New Button With {.Parent = Me, .Text = "Play A", .Location = New Point(20, 20)}
Private WithEvents ButtonB As New Button With {.Parent = Me, .Text = "Play B", .Location = New Point(120, 20)}
Private WithEvents RepeatCb As New CheckBox With {.Parent = Me, .Text = "Repeat", .Location = New Point(220, 20)}
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then TextBox1.BackColor = Color.Red
End Sub
Private Sub ButtonA_Click_1(sender As Object, e As EventArgs) Handles ButtonA.Click
Dim xa() As Byte = IO.File.ReadAllBytes("c:\<MY PATH>\1.wav")
Dim data As New IO.MemoryStream(xa) 'data stream for the buffer
Wave1.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(data))))
Wave1.Volume = 0.5
Wave1.Play()
GC.SuppressFinalize(Me)
'Snds.Play("Note A")
End Sub
Private Sub ButtonB_Click(sender As Object, e As EventArgs) Handles ButtonB.Click
Dim xa() As Byte = IO.File.ReadAllBytes("c:\<MY PATH\3.wav")
Dim data As New IO.MemoryStream(xa) 'data stream for the buffer
Wave1.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(data))))
'If (key(1)==128) Then
Wave1.Volume = 0.5
'End If
Wave1.Play()
GC.SuppressFinalize(Me)
'Snds.Play("Note B")
End Sub
Private Sub RepeatCb_CheckedChanged(sender As Object, e As EventArgs) Handles RepeatCb.CheckedChanged
Snds.repeat = RepeatCb.Checked
End Sub
End Class
Public Class MultiSounds
Private Snds As New Dictionary(Of String, String)
Private sndcnt As Integer = 0
Public repeat As Boolean
<DllImport("winmm.dll", EntryPoint:="mciSendStringW")>
Private Shared Function mciSendStringW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszCommand As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszReturnString As System.Text.StringBuilder, ByVal cchReturn As UInteger, ByVal hwndCallback As IntPtr) As Integer
End Function
Public Function AddSound(ByVal SoundName As String, ByVal SndFilePath As String) As Boolean
If SoundName.Trim = "" Or Not IO.File.Exists(SndFilePath) Then Return False
'to repeat must use type mpegvideo ????
If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " type mpegvideo alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
'If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
Snds.Add(SoundName, "Snd_" & sndcnt.ToString)
sndcnt += 1
Return True
End Function
Public Function Play(ByVal SoundName As String) As Boolean
If Not Snds.ContainsKey(SoundName) Then Return False
mciSendStringW("seek " & Snds.Item(SoundName) & " to start", Nothing, 0, IntPtr.Zero)
If repeat Then
If mciSendStringW("play " & Snds.Item(SoundName) & " repeat", Nothing, 0, IntPtr.Zero) <> 0 Then Return False
Else
If mciSendStringW("play " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
End If
Return True
End Function
Public Function [Stop](ByVal SoundName As String) As Boolean
If Not Snds.ContainsKey(SoundName) Then Return False
If mciSendStringW("stop " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
Return True
End Function
End Class
Thanks in advance.
Continue reading...
I'm stuck at a position where I'm creating a VB .NET standalone application using VS 2017.
I've made a little progress in playing 2 audio files on clicking a button among the several others without stopping the previously playing WAV audio file.
But now I want to achieve a something where a single button should play an audio file (which I now have already come up with) but on clicking the same button on the application, the sound that it was playing by the click of the same old button should not be interrupted.
I'm completely a beginner to .NET and still have come up with this code so far. Help me with this new problem of mine. Ignore the comments.
Imports System.Runtime.InteropServices
Public Class Form1
Private Wave1 As New NAudio.Wave.WaveOut
Private Snds As New MultiSounds
Private WithEvents ButtonA As New Button With {.Parent = Me, .Text = "Play A", .Location = New Point(20, 20)}
Private WithEvents ButtonB As New Button With {.Parent = Me, .Text = "Play B", .Location = New Point(120, 20)}
Private WithEvents RepeatCb As New CheckBox With {.Parent = Me, .Text = "Repeat", .Location = New Point(220, 20)}
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then TextBox1.BackColor = Color.Red
End Sub
Private Sub ButtonA_Click_1(sender As Object, e As EventArgs) Handles ButtonA.Click
Dim xa() As Byte = IO.File.ReadAllBytes("c:\<MY PATH>\1.wav")
Dim data As New IO.MemoryStream(xa) 'data stream for the buffer
Wave1.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(data))))
Wave1.Volume = 0.5
Wave1.Play()
GC.SuppressFinalize(Me)
'Snds.Play("Note A")
End Sub
Private Sub ButtonB_Click(sender As Object, e As EventArgs) Handles ButtonB.Click
Dim xa() As Byte = IO.File.ReadAllBytes("c:\<MY PATH\3.wav")
Dim data As New IO.MemoryStream(xa) 'data stream for the buffer
Wave1.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(data))))
'If (key(1)==128) Then
Wave1.Volume = 0.5
'End If
Wave1.Play()
GC.SuppressFinalize(Me)
'Snds.Play("Note B")
End Sub
Private Sub RepeatCb_CheckedChanged(sender As Object, e As EventArgs) Handles RepeatCb.CheckedChanged
Snds.repeat = RepeatCb.Checked
End Sub
End Class
Public Class MultiSounds
Private Snds As New Dictionary(Of String, String)
Private sndcnt As Integer = 0
Public repeat As Boolean
<DllImport("winmm.dll", EntryPoint:="mciSendStringW")>
Private Shared Function mciSendStringW(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszCommand As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal lpszReturnString As System.Text.StringBuilder, ByVal cchReturn As UInteger, ByVal hwndCallback As IntPtr) As Integer
End Function
Public Function AddSound(ByVal SoundName As String, ByVal SndFilePath As String) As Boolean
If SoundName.Trim = "" Or Not IO.File.Exists(SndFilePath) Then Return False
'to repeat must use type mpegvideo ????
If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " type mpegvideo alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
'If mciSendStringW("open " & Chr(34) & SndFilePath & Chr(34) & " alias " & "Snd_" & sndcnt.ToString, Nothing, 0, IntPtr.Zero) <> 0 Then Return False
Snds.Add(SoundName, "Snd_" & sndcnt.ToString)
sndcnt += 1
Return True
End Function
Public Function Play(ByVal SoundName As String) As Boolean
If Not Snds.ContainsKey(SoundName) Then Return False
mciSendStringW("seek " & Snds.Item(SoundName) & " to start", Nothing, 0, IntPtr.Zero)
If repeat Then
If mciSendStringW("play " & Snds.Item(SoundName) & " repeat", Nothing, 0, IntPtr.Zero) <> 0 Then Return False
Else
If mciSendStringW("play " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
End If
Return True
End Function
Public Function [Stop](ByVal SoundName As String) As Boolean
If Not Snds.ContainsKey(SoundName) Then Return False
If mciSendStringW("stop " & Snds.Item(SoundName), Nothing, 0, IntPtr.Zero) <> 0 Then Return False
Return True
End Function
End Class
Thanks in advance.
Continue reading...