PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] How to play sound by time with a button click-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] How to play sound by time with a button click

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    13

    Resolved [RESOLVED] How to play sound by time with a button click

    Hi.

    If the computer time is 20:16, I want to play "D:/mp3/20.16.mp3" when I click the button.
    If the computer time is 20:17, I want to play "D:/mp3/20.17.mp3" when I click the button.
    If the computer time is 20:18, I want to play "D:/mp3/20.18.mp3" when I click the button.
    ...

    How can I do that with vb net? Seconds don't matter (It should play the same mp3 file at 20:16:01 and at 20:16:59.). Thank you for your answer in advance.
    Last edited by teos983; Jul 20th, 2019 at 12:15 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,284

    Re: How to play sound by time with a button click

    There are any number of ways to accomplish this. Do you need help with both parts, i.e. how to check the time and how to play the mp3?

    For checking the time, I guess I would just go the route of checking the hour, and if the hour matches, then check the minutes using a Case statement or series of If... ElseIf
    Since the time here is after 1 in the afternoon, I used this for a test.
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
        Dim t As Date = Now
    
        If t.Hour = 13 Then
          If t.Minute = 25 Then
            Debug.Print("Play 25")
          ElseIf t.Minute = 25 Then
            Debug.Print("Play 26")
          End If
        End If
      End Sub
    Of course you would replace the Debug.Print with a statement to play the mp3 you want for that minute.

    With single statement Cases, I often just use a : to put the statement on the same line for compactness.
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
        Dim t As Date = Now
    
        If t.Hour = 13 Then
          Select Case t.Minute
            Case 25 : Debug.Print("Play 25")
            Case 26 : Debug.Print("Play 26")
            Case 27 : Debug.Print("Play 27")
            Case Else : Debug.Print("Play default sound")
          End Select
        End If
      End Sub
    If you only need to play one sound at a given time, you could just use the Audio.Play method.
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
        Dim t As Date = Now
    
        If t.Hour = 20 Then
          Select Case t.Minute
            Case 16 : My.Computer.Audio.Play("D:/mp3/20.16.mp3")
            Case 17 : My.Computer.Audio.Play("D:/mp3/20.17.mp3")
            Case 18 : My.Computer.Audio.Play("D:/mp3/20.18.mp3")
            Case Else : My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Exclamation)
          End Select
        End If
      End Sub
    Last edited by passel; Jul 20th, 2019 at 01:45 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    13

    Re: How to play sound by time with a button click

    Hi passel, thanks a lot for this information. It works perfectly with wav files but when I tried the code below, nothing happened:

    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
        Dim t As Date = Now
    
        If t.Hour = 20 Then
          Select Case t.Minute
            Case 16 : My.Computer.Audio.Play("D:/mp3/20.16.mp3")
            Case 17 : My.Computer.Audio.Play("D:/mp3/20.17.mp3")
            Case 18 : My.Computer.Audio.Play("D:/mp3/20.18.mp3")
            Case Else : My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Exclamation)
          End Select
        End If
      End Sub
    I think I should learn how to play mp3 files in vb net. Do you have any suggestions?
    Last edited by teos983; Jul 20th, 2019 at 04:26 PM.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,284

    Re: How to play sound by time with a button click

    There are a number of choices there as well.
    I've usually either used Windows Media Player, or used the MultiMediaInterface API, which has been part of Windows probably since the early 1990's.

    For an example, I guess I'll go with the Windows Media Player.

    Look in the Components section of the Toolbox (where you pick controls and components to add to a form)
    Generally, at the bottom of the Components section should be where "Windows Media Player" would show up.
    It isn't usually there if you haven't used it before so you have to add it.
    To add it, right click within the toolbox and select "Choose Items".
    This will populate a dialog where you can add things to the Toolbox. It may take a while to show up while it is building the list and some components it finds may have missing libraries, so it will report that, delaying the presentation of the dialog, as you have to acknowledge the error. And if the message box "pops up" behind your IDE, you may be sitting there wondering whether anything is happening.

    Once the dialog is up, select the COM Components tab, and then select "Windows Media Player.
    This should add Windows Media Player to your Components section of the toolbox.

    I then added the WindowsMediaPlayer component to the form, and you get a control probably named "axWindowsMediaPlayer1".
    I set the visible property to False, since we don't need to see it to play our sounds.
    I also changed the name to something more manageable, i.e. in the code below you see I reference MediaPlayer, not axWindowsMediaPlayer1.

    Then, in my case, I looked for some valid mp3 files on this computer (don't have a lot), and moved some of them to a common directory for convenience.

    Add a button to the form, and initialize the list with the path of the various files to play. You'll need to modify the list to files you have, before running the example.

    In this example, each click of the button just plays the next song in the list, and it cycles back to the beginning.
    Code:
    Public Class Form1
    
      Private songList As New List(Of String)
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        songList.AddRange(
          {"c:\c\music\piano.mp3",
           "c:\c\music\piano2.mp3",
           "c:\c\music\track1.mp3"
          })
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Static idx As Integer
        mediaPlayer.URL = songList(idx)
        idx = (idx + 1) Mod songList.Count
      End Sub
    
    End Class
    Last edited by passel; Jul 20th, 2019 at 05:41 PM.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    387

    Re: [RESOLVED] How to play sound by time with a button click

    You might be able to use speach syth instead of playing files?

    Anywho, I side tracked into this example that uses speech syth to say the current system time through your computer speakers when you click the button. Lots of possibilities. When I first read your post I thought that might be what you wanted. The example also shows how to build a string that could be file names for the time.

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

    You will need a project ref to System.Speech as shown here.

    Name:  a.png
Views: 43
Size:  13.2 KB

    Code:
    'say the time
    Imports System.Speech.Synthesis
    
    Public Class Form5
        Private WithEvents Timer1 As New Windows.Forms.Timer With {.Interval = 500, .Enabled = True}
        Private Label1 As New Label With {.Parent = Me, .Location = New Point(50, 50), .AutoSize = False,
            .Size = New Size(200, 32), .Font = New Font("tahoma", 24, FontStyle.Bold), .ForeColor = Color.AntiqueWhite}
        Private WithEvents Button1 As New Button With {.Parent = Me, .Location = New Point(100, 150),
             .ForeColor = Color.AntiqueWhite}
        Private PlayVoice As String = ""
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            BackColor = Color.Maroon
            Text = "Speach Time Example"
    
            Dim b As New System.Speech.Synthesis.SpeechSynthesizer
            For Each voice As System.Speech.Synthesis.InstalledVoice In b.GetInstalledVoices()
                PlayVoice = voice.VoiceInfo.Name
                Exit For
            Next
    
            If PlayVoice = "" Then
                MessageBox.Show("A voice could not be initialized")
                Button1.Enabled = False
                Button1.Text = "Off"
            Else
                Button1.Text = "Say Time"
            End If
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using speaker As New SpeechSynthesizer()
                speaker.Rate = 0
                speaker.Volume = 100
                speaker.SelectVoice(PlayVoice)
                Dim tm As DateTime = Now
                speaker.Speak("The Time Is " & tm.ToString("mm") & " minutes after " & tm.ToString("%h"))
            End Using
        End Sub
    
        Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Label1.Text = Now.ToLongTimeString
        End Sub
    End Class

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    13

    Re: [RESOLVED] How to play sound by time with a button click

    Hi passel. With your last message, everything's done. Now I can play mp3 files with no error. Thank you very much.

    Hi tommytwotrain. Thank you for understanding what I want to do. I've thought about "text to speech" option before. But my native language is Turkish. When it comes to text to speech, unfortunately Windows 10 does not support Turkish language at this time. But you make me very happy with this code, really thank you. I'm searching the web for external text to speech libraries. I haven't found anything yet but I think I can find.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,599

    Re: How to play sound by time with a button click

    Quote Originally Posted by passel View Post
    If you only need to play one sound at a given time, you could just use the Audio.Play method.
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
        Dim t As Date = Now
    
        If t.Hour = 20 Then
          Select Case t.Minute
            Case 16 : My.Computer.Audio.Play("D:/mp3/20.16.mp3")
            Case 17 : My.Computer.Audio.Play("D:/mp3/20.17.mp3")
            Case 18 : My.Computer.Audio.Play("D:/mp3/20.18.mp3")
            Case Else : My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Exclamation)
          End Select
        End If
      End Sub
    Rather than that Select Case, I would just construct the file name from the time and then see whether a matching file exists:
    vb.net Code:
    1. Dim filePath = $"D:\mp3\{Date.Now:HH.mm}.mp3"
    2.  
    3. If File.Exists(filePath) Then
    4.     'Play the file.
    5. Else
    6.     'Do whatever is appropriate.
    7. End If

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