NAudio在运行时更改音量 [英] NAudio change volume in runtime

查看:494
本文介绍了NAudio在运行时更改音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时更改音频流的音量. 我使用以下代码: 单个公共卷= 0.01 Dim Wave1作为新的NAudio.Wave.WaveOut

I would like change the volume of my audiostream in runtime. I use this code: Public Volume as Single = 0.01 Dim Wave1 As New NAudio.Wave.WaveOut

Dim xa() As Byte = IO.File.ReadAllBytes("C:\Song - Come Out and Play.wav")

Sub PlaySound()

    Dim data As New IO.MemoryStream(xa)

    Wave1.Init(New NAudio.Wave.BlockAlignReductionStream(NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(New NAudio.Wave.WaveFileReader(data))))

    Wave1.Volume = Volume

    Wave1.Play()

End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PlaySound()
End Sub

但是如何在运行时更改卷? 当我用

But how can i change the Volume in runtime? It doesn´t change, when i click a button with

Volume = 2.0

为什么?

谢谢.

第二个问题:我该如何更改此代码才能播放MP3而不是WAV? 因为WAV很大.

Second problem: How must i change this code, to play MP3 instead of WAV? Because WAV is to large..

都感谢:)

对不起,我的英语不好.

Sorry for my bad English.

致谢René:)

推荐答案

第一个有关音量的问题已由Mark回答-音量在0.0 <= volume <= 1.0范围内,因此将音量设置为2.0无效.

First question about volume was answered by Mark - Volume is in the range 0.0 <= volume <= 1.0, so setting the volume to 2.0 is invalid.

关于如何使用MP3文件...

As to how to use MP3 files...

如果输入的数据是MP3而不是WAV,则可以在代码中用Mp3FileReader替换WaveFileReader.如果数据始终来自文件,则可以改用new AudioFileReader(filename)并让其确定压缩格式是什么.

You can replace the WaveFileReader with Mp3FileReader in your code if the data you are feeding in is MP3 instead of WAV. If the data is always coming from a file you could use new AudioFileReader(filename) instead and let it work out what the compression format is.

以下是您使用MP3而不是WAV的代码:

Here's your code with MP3 instead of WAV:

Public Volume as Single = 0.01

Dim Wave1 As New NAudio.Wave.WaveOut

Dim xa() As Byte = IO.File.ReadAllBytes("C:\Song - Come Out and Play.mp3")

Sub PlaySound()

    Dim data As New IO.MemoryStream(xa)

    Wave1.Init( _
        New NAudio.Wave.BlockAlignReductionStream( _
            NAudio.Wave.WaveFormatConversionStream.CreatePcmStream( _
                New NAudio.Wave.Mp3FileReader(data) _
        )))

    Wave1.Volume = Volume

    Wave1.Play()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PlaySound()
End Sub

这篇关于NAudio在运行时更改音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆