C#windows形成应用程序卷滑块

时间:2016-03-31 10:57:17

标签: c# windows winforms wav soundplayer

我有一个使用声音播放器播放.wav文件的应用程序,我查了一下,找不到改变播放音量的方法。我正在寻找的是要么改变音量。通过程序独立存档或有一个滑块来改变Windows音量混合器中窗口本身的音量。谢谢!

/*
create table UserDocuments (user_id int, document_id int, [date] date)
insert into UserDocuments values 
(1,           1,             '2016-01-01'),
(1,           2,             '2016-01-01'),
(1,           3,             '2016-01-02'),
(2,           4,             '2016-01-01'),
(2 ,          5,             '2016-01-02'),
(3,           6,             '2016-01-02'),
(3,           7,             '2016-01-02'),
(3,           8,             '2016-01-02'),
(3,           9,             '2016-01-03'),
(3,          10,             '2016-01-03'),
(3,          11,             '2016-01-04'),
(3,           9 ,            '2016-01-04')
*/

select
    [date], [user_id], count(*) document_count
from UserDocuments
group by [date], [user_id]
order by [date], [user_id]

1 个答案:

答案 0 :(得分: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);
      }
   }
}

希望它有所帮助。