dcsimg
Results 1 to 12 of 12

Thread: [RESOLVED] Play sound in VB.NET 2010 When Key pressed

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Resolved [RESOLVED] Play sound in VB.NET 2010 When Key pressed

    I have searched a lot, but cannot find a solution in VB.NET 2010 for playing a sound (.WAV) while a button is pressed and hold. When the button is released, the sound must stop immediatly.
    The Wav files I have have a duration of 3 sec. But when I relase the button after 0,5 sec, it must stop and not finish the wav File.

    Who knows a suggestion?

  2. #2
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    77

    Re: Play sound in VB.NET 2010 When Key pressed

    Pressed and hold is a strict condition?

    Code:
    Public Class Form1
    Dim WithEvents timer As New Timer
    Dim milliseconds As Integer
    
    
    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
        timer.Start()
    End Sub
    
    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
        timer.Stop()
        Label1.Text = "Button held down for: " & milliseconds & " milliseconds"
        If milliseconds >= 10 then 'Mouse has been down for one second
          DoSomething()
        End If
    End Sub
    
    Private Sub EggTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
        milliseconds += 1
    End Sub
    
    
    End Class
    Code:
    
    My.Computer.Audio.Play(Filename, AudioPlayMode.Background)
    I wont test it, but this could be what you have searched for...

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Play sound in VB.NET 2010 When Key pressed

    Thanks, it comes closer.
    Hoewever it works the opposite way. It plays when the button is released (mouse up) and then it plays the whole .wav file.

    I want to play when a button is pressed and hold and stop the play when released (exactly just like an Organ).

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Play sound in VB.NET 2010 When Key pressed

    Thanks, it comes closer.
    Hoewever it works the opposite way. It plays when the button is released (mouse up) and then it plays the whole .wav file.

    I want to play when a button is pressed and hold and stop the play when released (exactly just like an Organ).

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Play sound in VB.NET 2010 When Key pressed

    I found it!
    At MouseUp event: My.Computer.Audio.Stop()

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Play sound in VB.NET 2010 When Key pressed

    The only issue I still have in this case is that the timer is rather unpredictable. With the timer I have to set the duration of the tones. When the timer exceeds the chosen duration the music has to stop.
    The interval is 1ms. The duration of the tones goes from 10ms to 250ms. Perhaps is 1ms too less for the timer. Should I work with timespan? Is that a better solution?

  7. #7
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    410

    Re: Play sound in VB.NET 2010 When Key pressed

    Quote Originally Posted by bavelt View Post
    The only issue I still have in this case is that the timer is rather unpredictable. With the timer I have to set the duration of the tones. When the timer exceeds the chosen duration the music has to stop.
    The interval is 1ms. The duration of the tones goes from 10ms to 250ms. Perhaps is 1ms too less for the timer. Should I work with timespan? Is that a better solution?

    "I want to play when a button is pressed and hold and stop the play when released (exactly just like an Organ). "

    What is the timer for? I am not sure the question was clear at that timer example.

    This example plays the wav file while the mouse is pressed and stops play when the mouse is released - just like the keys on an organ but uses the mouse.

    The example makes the controls just cut and paste the code into an empty form to run. Change the form name as required.

    Code:
    'play wav while button pressed
    Public Class Form9
        Private WithEvents Player1 As New System.Media.SoundPlayer With
            {.SoundLocation = "C:\test sounds\cranekick.wav"}
        Private WithEvents PlayBtnA As New Button With {.Parent = Me,
            .Text = "Play", .Location = New Point(150, 150), .ForeColor = Color.AntiqueWhite,
            .Size = New Size(80, 40), .Font = New Font("tahoma", 14, FontStyle.Bold)}
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            BackColor = Color.Maroon
        End Sub
    
        Private Sub PlayBtnA_MouseDown(sender As Object, e As MouseEventArgs) Handles PlayBtnA.MouseDown
            Player1.Play()
            PlayBtnA.Text = "Stop"
        End Sub
    
        Private Sub PlayBtnA_MouseUp(sender As Object, e As MouseEventArgs) Handles PlayBtnA.MouseUp
            Player1.Stop()
            PlayBtnA.Text = "Play"
        End Sub
    End Class

    PS you cant time anything faster than about 10 ms on a pc with a win forms timer only.
    Last edited by tommytwotrain; Aug 20th, 2019 at 05:53 AM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Play sound in VB.NET 2010 When Key pressed

    I was not fully complete with my post. Sorry for that.
    The keypress and play audio works now fine.
    In the same programm however I have a second functionality; I can add the selected tone with the duration into a .SQL database.
    But when I have to replay the stored tones with their duration, they must play with the right duration.
    f.e. A4, 63mS, G4, 250ms, D4, 115 ms.

    I used the timer for that with an interval of 1ms. Let's say: first tone, the wav file with tone A4 (440Hz) has an duration of 250 ms. While Playing the timer runs with 1ms interval and when it exceeds 250 ms I want to stop playing.
    But for one or other reason the timer.tick seems not to tick unless the play of the .wav is completed.


    The programm until so far:
    Imports System.Data.SqlClient

    Public Class Startform
    Public Shared dsTonen As New DataSet
    Public Shared daTonen As SqlDataAdapter

    Dim milliseconds As Integer
    Dim Duur As Integer
    Public Shared Location As String
    Public Shared ExecutableDirectory As String


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Location = System.Reflection.Assembly.GetEntryAssembly.Location
    ExecutableDirectory = System.IO.Path.GetDirectoryName(Location)

    Lezen()

    With DgvTonen
    .DataSource = dsTonen.Tables("Tonen")
    .Columns(0).Width = 0
    .Columns(0).Visible = False
    .Columns(1).Width = 50
    .Columns(2).Width = 50
    .Columns(3).Width = 50
    End With

    End Sub

    Protected Sub lbtnA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles C4.MouseDown, C4H.MouseDown, D4.MouseDown, D4H.MouseDown, E4.MouseDown, F4.MouseDown, F4H.MouseDown, G4.MouseDown,
    G4H.MouseDown, A4.MouseDown, A4H.MouseDown, B4.MouseDown, C5.MouseDown, C5H.MouseDown, D5.MouseDown, D5H.MouseDown,
    E5.MouseDown, F5.MouseDown, F5H.MouseDown, G5.MouseDown, G5H.MouseDown
    Dim btn As Object = CType(sender, Button)

    Select Case btn.name
    Case "C4"
    My.Computer.Audio.Play(ExecutableDirectory & "\C4.Wav", AudioPlayMode.Background)
    Case "C4H"
    My.Computer.Audio.Play(ExecutableDirectory & "\C4H.Wav", AudioPlayMode.Background)
    Case "D4"
    My.Computer.Audio.Play(ExecutableDirectory & "\D4.Wav", AudioPlayMode.Background)
    Case "D4H"
    My.Computer.Audio.Play(ExecutableDirectory & "\D4H.Wav", AudioPlayMode.Background)
    Case "E4"
    My.Computer.Audio.Play(ExecutableDirectory & "\E4.Wav", AudioPlayMode.Background)
    Case "F4"
    My.Computer.Audio.Play(ExecutableDirectory & "\F4.Wav", AudioPlayMode.Background)
    Case "F4H"
    My.Computer.Audio.Play(ExecutableDirectory & "\F4H.Wav", AudioPlayMode.Background)
    Case "G4"
    My.Computer.Audio.Play(ExecutableDirectory & "\G4.Wav", AudioPlayMode.Background)
    Case "G4H"
    My.Computer.Audio.Play(ExecutableDirectory & "\G4H.Wav", AudioPlayMode.Background)
    Case "A4"
    My.Computer.Audio.Play(ExecutableDirectory & "\A4.Wav", AudioPlayMode.Background)
    Case "A4H"
    My.Computer.Audio.Play(ExecutableDirectory & "\A4H.Wav", AudioPlayMode.Background)
    Case "B4"
    My.Computer.Audio.Play(ExecutableDirectory & "\B4.Wav", AudioPlayMode.Background)
    Case "C5"
    My.Computer.Audio.Play(ExecutableDirectory & "\C5.Wav", AudioPlayMode.Background)
    Case "C5H"
    My.Computer.Audio.Play(ExecutableDirectory & "\C5H.Wav", AudioPlayMode.Background)
    Case "D5"
    My.Computer.Audio.Play(ExecutableDirectory & "\D5.Wav", AudioPlayMode.Background)
    Case "D5H"
    My.Computer.Audio.Play(ExecutableDirectory & "\D5H.Wav", AudioPlayMode.Background)
    Case "E5"
    My.Computer.Audio.Play(ExecutableDirectory & "\E5.Wav", AudioPlayMode.Background)
    Case "F5"
    My.Computer.Audio.Play(ExecutableDirectory & "\F5.Wav", AudioPlayMode.Background)
    Case "F5H"
    My.Computer.Audio.Play(ExecutableDirectory & "\F5H.Wav", AudioPlayMode.Background)
    Case "G5"
    My.Computer.Audio.Play(ExecutableDirectory & "\G5.Wav", AudioPlayMode.Background)
    Case "G5H"
    My.Computer.Audio.Play(ExecutableDirectory & "\G5H.Wav", AudioPlayMode.Background)
    End Select
    End Sub

    Private Sub BtnAll_MouseDown(sender As System.Object, e As System.EventArgs) Handles C4.MouseUp, C4H.MouseUp, D4.MouseUp, D4H.MouseUp, E4.MouseUp, F4.MouseUp, F4H.MouseUp, G4.MouseUp,
    G4H.MouseUp, A4.MouseUp, A4H.MouseUp, B4.MouseUp, C5.MouseUp, C5H.MouseUp, D5.MouseUp, D5H.MouseUp,
    E5.MouseUp, F5.MouseUp, F5H.MouseUp, G5.MouseUp, G5H.MouseUp
    My.Computer.Audio.Stop()
    End Sub

    Private Sub BtnPlay_Click(sender As System.Object, e As System.EventArgs) Handles BtnPlay.Click
    Dim Rij As Integer = 0
    Dim Noot As String
    Timer1.Interval = 1

    For Each Trow As DataRow In dsTonen.Tables(0).Rows
    DgvTonen.Rows(Rij).Cells(1).Style.BackColor = Color.Red

    Noot = ExecutableDirectory & "" & Trow("Toon") & ".Wav"
    Timer1.Stop()
    Duur = Trow("Duur")
    milliseconds = 0

    Timer1.Start()

    My.Computer.Audio.Play(Noot, AudioPlayMode.Background)
    DgvTonen.Rows(Rij).Cells(1).Style.BackColor = Color.White
    Me.Show()
    Application.DoEvents()
    Rij += 1
    If Rij > DgvTonen.Rows.Count - 1 Then Exit For
    Next
    End Sub

    Private Sub BtnLees_Click(sender As System.Object, e As System.EventArgs) Handles BtnLees.Click
    Lezen()
    End Sub

    Private Sub Lezen()
    dsTonen.Clear()
    daTonen = New SqlDataAdapter("SELECT * FROM TONEN", My.Settings.SongsConnectionString)
    daTonen.Fill(dsTonen, "Tonen")
    End Sub

    Private Sub TStripBtnClose_Click(sender As System.Object, e As System.EventArgs) Handles TStripBtnClose.Click
    Application.Exit()
    End Sub

    Private Sub TStripBtnSave_Click(sender As System.Object, e As System.EventArgs) Handles TStripBtnSave.Click
    Dim cb = New SqlCommandBuilder(daTonen)
    Try
    daTonen.Update(dsTonen, "Tonen")
    Catch ex As Exception
    MsgBox("Update kan niet worden uitgevoerd")
    End Try
    Lezen()
    End Sub

    Private Sub EggTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    milliseconds += 1
    If milliseconds > Duur Then
    Timer1.Stop()
    My.Computer.Audio.Stop()
    End If
    End Sub

    End Class
    Last edited by bavelt; Aug 20th, 2019 at 09:13 AM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Play sound in VB.NET 2010 When Key pressed

    PS How can I present the code in a separate window in the post as you did?

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,333

    Re: Play sound in VB.NET 2010 When Key pressed

    You can type in Code tags manually, or you could just select the code in the window and then click on the button above the Reply window that has a # in it, which will add the code tags for you. I usually just type the tags in manually before I paste or type the code in between the tags, because that is how I started, but the # button seems like it should be convenient as well. The button to the left of it says "VB", but usually looks like "VE", and that is another code format window. It will prompt for a language to format. I assume you can ignore the language, or type in VB or whatever. I don't use that format very often.

  11. #11
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    410

    Re: Play sound in VB.NET 2010 When Key pressed

    Here is a timed example using a button with mouse. It uses a stopwatch for the timing which is a bit more flexible and fun?

    You can adapt it for your project.

    Code:
    'play wav for duration
    Public Class Form10
        Private WithEvents Timer1 As New Timer With {.Interval = 1}
        Private WithEvents SW As New Stopwatch
        Private WithEvents PlayBtnA As New Button With {.Parent = Me,
            .Text = "Play", .Location = New Point(150, 150), .ForeColor = Color.AntiqueWhite,
            .Size = New Size(80, 40), .Font = New Font("tahoma", 14, FontStyle.Bold)}
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                BackColor = Color.Maroon
            End Sub
    
        Private Sub PlayBtnA_MouseDown(sender As Object, e As MouseEventArgs) Handles PlayBtnA.MouseDown
            My.Computer.Audio.Play("C:\test sounds\cranekick.wav")
            SW.Reset()
            SW.Start()
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim duration As Integer = 467  'milliseconds
            Text = SW.ElapsedMilliseconds.ToString
    
            If duration < SW.ElapsedMilliseconds Then
                My.Computer.Audio.Stop()
                SW.Stop()
                Timer1.Stop()
            End If
        End Sub
    End Class

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    8

    Re: Play sound in VB.NET 2010 When Key pressed

    Thank you so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width