2019年12月19日 星期四

電腦發音、音效、音樂

Public Class Form1
    Declare Function Beep Lib "kernel32" (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer
註:將beep改成Console.Beep就不用上面這一Declare Function Beep......

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '救護車聲
        Dim myAlert As New Threading.Thread(AddressOf Alert)
        Dim I As Short
        For I = 0 To 10
            Beep(1000, 500)
            Beep(2000, 1000)
        Next
    End Sub
  
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '炸彈掉下聲
        Dim I As Short
        For I = 2500 To 750 Step -10
            Beep(I, 50)
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '生日快樂
        Dim i() As Integer = {392, 392, 440, 392, 523, 494, 392, 392, 440, 392, 587, 523, 392, 392, 784, 659, 523, 494, 440, 698, 698, 659, 523, 587, 523}
        Dim j() As Integer = {375, 125, 500, 500, 500, 1000, 375, 125, 500, 500, 500, 1000, 375, 125, 500, 500, 500, 500, 1000, 375, 125, 500, 500, 500, 1000}
        Dim ak As Integer
        For ak = 0 To i.Length - 1
            Beep(i(ak), j(ak))
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '警報聲
        Dim I As Short
        For I = 0 To 20

            Beep(75, 200)
            Beep(400, 200)
        Next
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        '單聲
        Console.Beep()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim myAlert As New Threading.Thread(AddressOf Alert)
        myAlert.Start()
    End Sub
    Sub Alert()
        Dim I As Short
        For I = 2500 To 750 Step -10
            Beep(I, 50)
            'Beep(I, 100)
        Next
    End Sub
End Class
-------------------------------------------------------------------------------------------------------------
●MSDN的描述:The Beep function generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes.
●小站已測試平台:Windows9x、Windows NT4、Windows 2000、Windows XP
●建議宣告法: 'VB6
Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
--------------------------------------------------------------------------------
 'VB.Net宣告
Declare Function Beep Lib "kernel32" (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer 
●用法範 例:
●VB6警車聲
Private Sub Form_Activate()
    For I = 0 To 10
        Beep 75, 200
        Beep 400, 200
        DoEvents
    Next
End Sub 
●VB.Net警車聲
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myAlert As New Threading.Thread(AddressOf Alert)
    myAlert.Start()
End Sub
Sub Alert()
    Dim I As Short
    For I = 0 To 10
        Beep(75, 200)
        Beep(400, 200)
    Next
End Sub 

●VB6卡通墜落聲
Private Sub Form_Activate()
    For I = 2500 To 750 Step -10
        Beep I, 50
        DoEvents
    Next
End Sub 
●VB.Net警車聲
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim myAlert As New Threading.Thread(AddressOf Alert)
    myAlert.Start()
End Sub
Sub Alert()
    Dim I As Short
    For I = 2500 To 750 Step -10
        Beep(I, 50)
    Next
End Sub 

●dwFreq→頻率(Hz)
●dwDuration→播放時間(千分之一秒)

沒有留言:

張貼留言