2016-03-31 42 views
3

我有一个使用Soundplayer播放.wav文件的应用程序,我查了一下,找不到改变播放音量的方法。我在寻找的是通过程序独立更改文件的音量,或者使用滑块在Windows音量混音器中更改窗口本身的音量。谢谢!C#窗体窗体应用程序音量滑块

public void loadSound() 
{ 
    sp.Load(); 
    sp.Play(); 
} 

private void timer1_Tick(object sender, EventArgs e) 
{  
    if (BarTimer.Value < BarTimer.Maximum) 
    { 
     BarTimer.Value = BarTimer.Value + 1; 
    } 

    if(BarTimer.Value==BarTimer.Maximum) 
    { 
     loadSound(); 
     timer1.Stop(); 
     BarTimer.Value = BarTimer.Minimum; 
    } 
} 
+1

你用什么方法播放wav文件?请发布您的代码。 – auburg

+0

什么是sp?你使用什么媒体API? – auburg

+0

我正在使用system.media; – dvs

回答

4

我只在MSDN上发现了这个:Attenuating SoundPlayer Volume

它使用waveOutGetVolumewaveOutSetVolume功能。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace VolumeControl 
{ 
    public partial class Form1 : Form 
    { 
     [DllImport("winmm.dll")] 
     public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume); 

     [DllImport("winmm.dll")] 
     public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume); 

     public Form1() 
     { 
     InitializeComponent(); 
     // By the default set the volume to 0 
     uint CurrVol = 0; 
     // At this point, CurrVol gets assigned the volume 
     waveOutGetVolume(IntPtr.Zero, out CurrVol); 
     // Calculate the volume 
     ushort CalcVol = (ushort)(CurrVol & 0x0000ffff); 
     // Get the volume on a scale of 1 to 10 (to fit the trackbar) 
     trackWave.Value = CalcVol/(ushort.MaxValue/10); 
     } 

     private void trackWave_Scroll(object sender, EventArgs e) 
     { 
     // Calculate the volume that's being set. BTW: this is a trackbar! 
     int NewVolume = ((ushort.MaxValue/10) * trackWave.Value); 
     // Set the same volume for both the left and the right channels 
     uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16)); 
     // Set the volume 
     waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels); 
     } 
    } 
} 

希望它有帮助。

+1

我看了一下,我不确定如何导入dll,但我会看看,谢谢! – dvs

+1

我编辑了你的答案以包含代码。 +1 –