有没有办法在 VB.net 中播放不是 console.bleep 的音符?

Posted

技术标签:

【中文标题】有没有办法在 VB.net 中播放不是 console.bleep 的音符?【英文标题】:Is there a way to play notes in VB.net that isn't console.bleep? 【发布时间】:2020-12-29 18:48:45 【问题描述】:

我正在创建一个用于间隔识别的程序,我真的希望输出的声音稍微......比 console.bleep 输出的声音更好 - 最好是钢琴声音或类似的东西。 这是我对 console.bleep 所做的,因此您可以了解我需要做什么:

Imports System.Threading
Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            IntervalClass.PlayInterval(11, 5)
      End Sub
End Class
Class IntervalClass

      Protected Shared Sub Play(interval() As Note)
            Dim n As Note
            For Each n In interval
                  If n.NoteTone = Tone.REST Then
                        Thread.Sleep(CInt(n.NoteDuration))
                  Else
                        Console.Beep(CInt(n.NoteTone), CInt(n.NoteDuration))
                  End If
            Next n
      End Sub

      Public Shared Sub PlayInterval(ByRef StartingNote As Integer, ByRef EndingNote As Integer)
            Dim Notes() As String = Tone.C3, Tone.Cs3, Tone.D3, Tone.Ds3, Tone.E3, Tone.F3, Tone.Fs3, Tone.G3, Tone.Gs3, Tone.A3, Tone.As3, Tone.B3, Tone.C4, Tone.Cs4, Tone.D4, Tone.Ds4, Tone.E4, Tone.F4, Tone.Fs4, Tone.G4, Tone.Gs4, Tone.A4, Tone.As4, Tone.B4, Tone.C5, Tone.Cs5, Tone.D5, Tone.Ds5, Tone.E5, Tone.F5, Tone.Fs5, Tone.G5, Tone.Gs5, Tone.A5, Tone.As5, Tone.B5, Tone.C6
            Dim PlayInterval As Note() = 
            New Note(Notes(StartingNote), Duration.HALF), New Note(Notes(EndingNote), Duration.HALF)
            Play(PlayInterval)
      End Sub

      Protected Enum Tone
            REST = 0
            C3 = 130.81
            Cs3 = 138.59
            D3 = 146.83
            Ds3 = 155.56
            E3 = 164.81
            F3 = 174.61
            Fs3 = 185
            G3 = 196
            Gs3 = 207.65
            A3 = 220
            As3 = 233.08
            B3 = 246.94
            C4 = 261.63
            Cs4 = 277.18
            D4 = 293.66
            Ds4 = 311.13
            E4 = 329.63
            F4 = 349.23
            Fs4 = 369.99
            G4 = 392
            Gs4 = 415.3
            A4 = 440
            As4 = 466.16
            B4 = 493.88
            C5 = 523.25
            Cs5 = 554.37
            D5 = 587.33
            Ds5 = 622.25
            E5 = 659.25
            F5 = 698.46
            Fs5 = 739.99
            G5 = 783.99
            Gs5 = 830.61
            A5 = 880
            As5 = 932.33
            B5 = 987.77
            C6 = 1046.5
      End Enum

      Protected Enum Duration
            WHOLE = 1600
            HALF = WHOLE / 2
            QUARTER = HALF / 2
            EIGHTH = QUARTER / 2
            SIXTEENTH = EIGHTH / 2
      End Enum
      Protected Structure Note
            Private toneVal As Tone
            Private durVal As Duration

            Public Sub New(frequency As Tone, time As Duration)
                  toneVal = frequency
                  durVal = time
            End Sub

            Public ReadOnly Property NoteTone() As Tone
                  Get
                        Return toneVal
                  End Get
            End Property

            Public ReadOnly Property NoteDuration() As Duration
                  Get
                        Return durVal
                  End Get
            End Property
      End Structure
End Class

有没有什么方法可以通过 console.bleep 或其他方法获得更好的声音? (最好我也可以将它们放在一个数组中,因为需要索引来调用正确的注释) 谢谢

【问题讨论】:

【参考方案1】:

如果您想使用预加载的声音,则可以利用 SystemSound 类:https://docs.microsoft.com/en-us/dotnet/api/system.media.systemsound

据我所知,您可以访问:

    Asterisk Beep Exclamation Hand Question

例如:

Dim sounds = SystemSounds.Asterisk, SystemSounds.Beep, SystemSounds.Exclamation, SystemSounds.Hand, SystemSounds.Question
sounds(0).Play() ' play asterisk sound

如果您想播放自定义文件,则可以利用 SoundPlayer 类:https://docs.microsoft.com/en-us/dotnet/api/system.media.soundplayer

一个例子是:

Dim sounds = New SoundPlayer("file1.wav"), New SoundPlayer("file2.wav"), New SoundPlayer("file3.wav")
sounds(0).Play() ' play file1.wav

【讨论】:

以上是关于有没有办法在 VB.net 中播放不是 console.bleep 的音符?的主要内容,如果未能解决你的问题,请参考以下文章

vb.net 中 如何实现 播放mp3文件

程序开发小记VB.NET音乐播放器

有没有办法在 VB.NET 中分配更多的 RAM?

如何在 vb.Net 中通过外部扬声器播放声音?

在 windows 中生成声音频率 - VB.Net

如何从 vb.net 的播放列表中显示媒体播放器正在播放的文件?