dcsimg
Results 1 to 6 of 6

Thread: sound

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    4

    sound

    hello,

    Is it possible to add if 5 buttons is clicked in row (which gives short sound effect), then another song goes off? (also time requires not more than 1sec between each click or result would be false?)

    like... if btn1, bt2 etc is clicked... then waterfall.wav is played?


    second:

    which format is best to use when it comes to music? i tried wav.. to big filesize.
    mp3 requires often this windows media player ddl for end user? the best would to keep file size as small as possible and no more than 1 exe in the end result.

    ty

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,728

    Re: sound

    Yeah, it can be done. You're going to need one or five variables at form scope (not in any method). Five variables will be easiest in some ways, but if the number may change, you might consider using a single Integer rather than five Booleans.

    Assuming that you are using five Booleans, then when each button is clicked, set one of the Booleans to True. You will also need a timer with an interval of 1000 (1 second). When each button is clicked, stop, then start, the timer. This will cause it to reset its interval. If the timer Tick event ever fires, then 1 second has elapsed since the last button click. Therefore, in the Tick event, stop the timer and clear all five Booleans.

    There is one other case you will need to deal with: Once the fifth Boolean has been set, you will not be needing the timer. Whether you should stop it or not is a bit hard to say. After all, the timer tick isn't going to do anything other than clear the Booleans and stop the timer, so you might just let it tick that one time after the fifth button has been clicked. It all comes down to whether or not you want the Booleans to be left in the set or cleared state (if you leave them set you will need some other means to clear them or else you won't be able to click them again). In any case, after any button is clicked, you would call a method that would check all five Booleans and play the sound if all five are True. If you want to leave them True, you'd also disable the timer in that method.
    My usual boring signature: Nothing

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,245

    Re: sound

    try this. I included an API function for playing MP3s:

    Code:
    Public Class Form1
    
        Dim clickStats(4) As clicked
        Dim clickedIndex As Integer = 0
    
        'Api to send the commands to the mci device.
    
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
        (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
         Integer, ByVal hwndCallback As Integer) As Integer
    
        Dim _fileToPlay As String = Chr(34) + "full path & filename.mp3" + Chr(34)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            clickStats = Array.ConvertAll(clickStats, Function(cs) New clicked)
        End Sub
    
        Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button1.Click
            mciSendString("stop myDevice", Nothing, 0, 0)
            mciSendString("close myDevice", Nothing, 0, 0)
            mciSendString("open " & _fileToPlay & " alias myDevice", Nothing, 0, 0)
            clickStats(clickedIndex Mod 5).btn = DirectCast(sender, Button)
            clickStats(clickedIndex Mod 5).timeClicked = Environment.TickCount
            If clickedIndex Mod 5 = 4 Then
                Dim sequential As Boolean = False
                Dim buttons() As Button = {Button1, Button2, Button3, Button4, Button5}
                For x As Integer = 0 To 4
                    If clickStats(x).btn Is buttons(x) Then
                        If x > 0 Then
                            If clickStats(x - 1).timeClicked > clickStats(x).timeClicked - 1000 Then
                                sequential = True
                            Else
                                sequential = False
                                Exit For
                            End If
                        End If
                    Else
                        sequential = False
                        Exit For
                    End If
                Next
                If sequential Then mciSendString("play myDevice", Nothing, 0, 0)
            End If
            clickedIndex += 1
        End Sub
    
    End Class
    
    Public Class clicked
        Public btn As Button
        Public timeClicked As Integer
    End Class
    Last edited by .paul.; Jan 14th, 2014 at 06:58 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    4

    Re: sound

    Quote Originally Posted by Shaggy Hiker View Post
    Yeah, it can be done. You're going to need one or five variables at form scope (not in any method). Five variables will be easiest in some ways, but if the number may change, you might consider using a single Integer rather than five Booleans.

    Assuming that you are using five Booleans, then when each button is clicked, set one of the Booleans to True. You will also need a timer with an interval of 1000 (1 second). When each button is clicked, stop, then start, the timer. This will cause it to reset its interval. If the timer Tick event ever fires, then 1 second has elapsed since the last button click. Therefore, in the Tick event, stop the timer and clear all five Booleans.

    There is one other case you will need to deal with: Once the fifth Boolean has been set, you will not be needing the timer. Whether you should stop it or not is a bit hard to say. After all, the timer tick isn't going to do anything other than clear the Booleans and stop the timer, so you might just let it tick that one time after the fifth button has been clicked. It all comes down to whether or not you want the Booleans to be left in the set or cleared state (if you leave them set you will need some other means to clear them or else you won't be able to click them again). In any case, after any button is clicked, you would call a method that would check all five Booleans and play the sound if all five are True. If you want to leave them True, you'd also disable the timer in that method.
    Hey thanks for the insight, I will be looking more into this.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2013
    Posts
    4

    Re: sound

    Thanks for your effort paul, am currently too newcomer to understand where I put the button sounds/mp3s.

    http://postimg.org/image/cr5enc8h3/ here is a pic of the grapichal thing I made for fun. the C-notes are clickable labels, and should play sound if clicked on.

    Dim _fileToPlay As String = Chr(34) + "full path & filename.mp3" + Chr(34) -> this would play after the fifth button?

    I will save this code so i can get better at it, thanks paul

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,245

    Re: sound

    Quote Originally Posted by zd2000 View Post
    Dim _fileToPlay As String = Chr(34) + "full path & filename.mp3" + Chr(34) -> this would play after the fifth button?
    yes after the 5th button clicked

Tags for this Thread

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