【问题标题】:How to change the volume of a wav resource using NAudio?如何使用 NAudio 更改 wav 资源的音量?
【发布时间】:2013-07-10 16:25:11
【问题描述】:

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

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

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

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

我用 Google 搜索了 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;

【问题讨论】:

    标签: .net vb.net winforms naudio wave


    【解决方案1】:

    如果您可以将资源保留为流,则可以使用WaveFileReader 加载它,然后将其传递给SampleChannel 以允许您调整音量。不需要MeteringSampleProvider

    【讨论】:

    • 谢谢,但我只能按照第一步:Public wavefile As New NAudio.Wave.WaveFileReader("C:\Success.wav"),我不知道下一步该怎么做.
    • 下一步与您的问题中显示的相同 - 将其传递给 SampleChannel 构造函数。然后确保将该采样通道传递到要播放的 WaveOut 中,否则设置其音量将无效
    【解决方案2】:

    扩展解决方案:

    #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
    

    【讨论】:

    • WaveOut.Volume 现已弃用。这个答案已经过时了。
    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多