如何使用NAudio更改wav资源的音量?

时间:2013-07-10 16:25:11

标签: .net vb.net winforms naudio wave

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

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

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

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

我用Google搜索了Naudio和其他StackOverFlow这样的问题,我注意到NAudio库可以做这个工作,问题是所有样本都在C#中,也是超专业编码所以我真的不明白我是怎么做的可以改变我的wav文件的音量。

我在VB.NET工作。

如果您需要其他信息,那么这里是NAudio lib: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;

2 个答案:

答案 0 :(得分:2)

如果您可以将资源保存为Stream,则可以使用WaveFileReader加载它,然后将其传递到SampleChannel以允许您调整音量。

答案 1 :(得分: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