2013-07-10 68 views
1

在我的应用程序中,我播放两个声音文件,它们是Wave文件,两个资源,一个用于“成功”操作,另一个用于发生“错误”。如何使用NAudio更改wav资源的音量?

所以对于打他们,我这样做:

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

现在,我想在我的应用程序添加一个选项来修改容积是wavefiles,我的意思是比原体积体积小(打他们如果用户想这样做的话)。

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

我正在使用VB.NET。

如果再在这里需要额外的信息是n音讯LIB:http://naudio.codeplex.com/releases/view/96875

这里是n音讯的中的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; 

回答

1

扩展解决方案:

#Region " NAudio " 

Public Class NAudio_Helper 

' [ NAudio ] 
' 
' // By Elektro [email protected] 
' 
' 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 
+0

WaveOut.Volume现已弃用。这个答案已经过时了。 – stricq

2

如果你能得到你的资源保持作为一个流,那么你可以使用一个WaveFileReader加载它,然后传递到SampleChannel,使您可以调节音量。不需要MeteringSampleProvider

+0

谢谢,但我只能按照第一步:公共wavefile作为新NAudio.Wave.WaveFileReader( “C:\ Success.wav”),我不知道是怎么下一步要做的。 – ElektroStudios

+1

下一步与您的问题中显示的相同 - 将其传递给SampleChannel构造函数。然后确保您将该样本通道传递到您的WaveOut中进行播放,或者将其音量设置为无效 –