簡體   English   中英

C#windows形成應用程序卷滑塊

[英]C# windows forms application volume slider

我有一個使用聲音播放器播放.wav文件的應用程序,我查了一下,找不到改變播放音量的方法。我正在尋找的是要么獨立地改變文件的音量。該程序或有一個滑塊來改變窗口音量混合器中窗口本身的音量。 謝謝!

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;
    }
 }

我只在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);
      }
   }
}

希望它有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM