C# Windows Forms 应用程序音量滑块

3

我有一个应用程序,使用声音播放器播放.wav文件。我查了一下,没有找到一种改变其播放音量的方法。我需要的是通过程序独立地改变文件的音量,或在Windows音量混音器中拥有一个滑块以改变窗口本身的音量。谢谢!

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
什么是sp?您正在使用哪个媒体API? - auburg
我正在使用 system.media; - dvs
1个回答

5

我只在MSDN上找到了这个: 减弱SoundPlayer音量

它使用了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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接