Reputation: 20464
In my application I play two sound files they are Wave files, two resources, one for a "Success" action and the other for "Error" ocurred.
So for play them I do this:
My.Computer.Audio.Play(My.Resources.Success, AudioPlayMode.Background)
Now I want to add an option in my app to modify the volume of that wavefiles, I mean to play them with less volume than the original volume (if the user want to do that).
I Googled it for Naudio and other StackOverFlow questions like mine and I've noticed the NAudio library can do this job, the problem is all the samples are in C#, also are ultra professional coded so really I did not understand how I can change the volume of my wav files.
I'm working in VB.NET.
If you need additional information then here is the NAudio lib: http://naudio.codeplex.com/releases/view/96875
And here is the interesting part of the DemoApp of NAudio, I think here is how the volume is increased or decreased ...but I'm not sure:
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;
Upvotes: 2
Views: 5436
Reputation: 20464
Extended Solution:
#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
Upvotes: 1
Reputation: 49482
If you can get hold of your resource as a Stream, then you can use a WaveFileReader
to load it, and then pass that into a SampleChannel
to allow you to adjust the volume. MeteringSampleProvider
is not needed.
Upvotes: 2