2

在我的应用程序中,我播放两个声音文件,它们是 Wave 文件,两个资源,一个用于“成功”操作,另一个用于“错误”。

所以为了玩它们,我这样做:

My.Computer.Audio.Play(My.Resources.Success, AudioPlayMode.Background)

现在我想在我的应用程序中添加一个选项来修改该波形文件的音量,我的意思是用比原始音量更小的音量来播放它们(如果用户想要这样做)。

我用谷歌搜索了 Naudio 和我的其他 StackOverFlow 问题,我注意到 NAudio 库可以完成这项工作,问题是所有示例都是 C# 中的,而且是超专业的编码所以我真的不明白如何改变我的 wav 文件的体积。

我在 VB.NET 中工作。

如果您需要更多信息,那么这里是 NAudio 库:http ://naudio.codeplex.com/releases/view/96875

这是NAudio的DemoApp中有趣的部分,我认为这是音量增加或减少的方式......但我不确定:

        namespace NAudioDemo.AudioPlaybackDemo

        this.fileWaveStream = plugin.CreateWaveStream(fileName);
        var waveChannel =  new SampleChannel(this.fileWaveStream, true);
        this.setVolumeDelegate = (vol) => waveChannel.Volume = vol;
        waveChannel.PreVolumeMeter += OnPreVolumeMeter;

        var postVolumeMeter = new MeteringSampleProvider(waveChannel);
        postVolumeMeter.StreamVolume += OnPostVolumeMeter;
4

2 回答 2

2

如果您可以将资源作为 Stream 获取,则可以使用 aWaveFileReader加载它,然后将其传递给 aSampleChannel以允许您调整音量。MeteringSampleProvider不需要。

于 2013-07-12T12:58:11.037 回答
1

扩展解决方案:

#Region " NAudio "

Public Class NAudio_Helper

' [ NAudio ]
'
' // By Elektro H@cker
'
' Instructions:
' 1. Add a reference for the "NAudio.dll" file into the project.
'
' Examples:
'
' Dim Stream As NAudio.Wave.WaveFileReader = New NAudio.Wave.WaveFileReader(File)
'
' Set_Volume(Stream, 0.5)
' Play_Sound(Stream, 1)
' Play_Sound(My.Resources.AudioFile)
' Play_Sound("C:\File.wav")


' Play Sound (File)
Private Sub Play_Sound(ByVal File As String, _
                       Optional ByVal Volume As Single = Nothing)

    Dim Wave As New NAudio.Wave.WaveOut

    Select Case File.Split(".").Last.ToLower
        Case "aiff"
            Wave.Init(New NAudio.Wave.AiffFileReader(File))
        Case "mp3"
            Wave.Init(New NAudio.Wave.Mp3FileReader(File))
        Case "wav"
            Wave.Init(New NAudio.Wave.WaveFileReader(File))
        Case Else
            Wave.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.AudioFileReader(File))))
    End Select

    If Not Volume = Nothing Then Wave.Volume = Volume
    Wave.Play()

End Sub

' Play Sound (MemoryStream)
Private Sub Play_Sound(ByVal Stream As IO.MemoryStream, _
                       Optional ByVal Volume As Single = Nothing)

    Dim Wave As New NAudio.Wave.WaveOut
    Wave.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(Stream))))
    If Not Volume = Nothing Then Wave.Volume = Volume
    Wave.Play()

End Sub

' Play Sound (Unmanaged MemoryStream)
Private Sub Play_Sound(ByVal Stream As IO.UnmanagedMemoryStream, _
                       Optional ByVal Volume As Single = Nothing)

    Dim Wave As New NAudio.Wave.WaveOut
    Wave.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(Stream))))
    If Not Volume = Nothing Then Wave.Volume = Volume
    Wave.Play()

End Sub

' Play Sound (NAudio Stream)
Private Sub Play_Sound(ByVal NAudio_Stream As Object, _
                       Optional ByVal Volume As Single = Nothing)

    Dim Wave As New NAudio.Wave.WaveOut
    Wave.Init(NAudio_Stream)
    If Not Volume = Nothing Then Wave.Volume = Volume
    Wave.Play()

End Sub

' Set Volume (NAudio Stream)
Private Function Set_Volume(ByVal NAudio_Stream As Object, ByVal Volume As Single) _
As NAudio.Wave.WaveOut

    Dim Wave As New NAudio.Wave.WaveOut
    Wave.Init(NAudio_Stream)
    Wave.Volume = Volume
    Return Wave

End Function

End Class

#End Region
于 2013-07-13T08:19:19.813 回答