2013-07-10 2 views
1

내 응용 프로그램에서는 웨이브 파일, 두 개의 리소스, 하나는 "성공"동작, 다른 하나는 "오류"에 대한 두 개의 사운드 파일을 재생합니다. 재생할 대한 그래서NAudio를 사용하여 wav 리소스의 볼륨을 변경하는 방법은 무엇입니까?

나는이 작업을 수행 : 그 wavefiles의

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

가 지금은 볼륨을 수정하는 내 애플 리케이션에 옵션을 추가하려면, 내가 원래 볼륨보다 작은 볼륨 (로 재생할 의미 사용자가 원하는 경우).

저는 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; 

답변

1

확장 솔루션 : 새로운 NAudio.Wave.WaveFileReader 공공 wavefile :

#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

는, 당신은 당신이 볼륨을 조절 할 수 있도록하기 위해 SampleChannel로 되었 다음을로드 할 WaveFileReader를 사용하고 있습니다. MeteringSampleProvider은 필요하지 않습니다.

+0

감사하지만 난 첫 번째 단계를 수행 할 수 : 방법 ("C \ Success.wav"), 나도 몰라 다음 단계. – ElektroStudios

+1

다음 단계는 질문에 표시된 것과 동일합니다. SampleChannel 생성자로 전달합니다. 그런 다음 해당 샘플 채널을 WaveOut에 전달하여 연주하거나 볼륨 설정을 적용하지 마십시오. –

관련 문제