gpt4 book ai didi

C# Windows 窗体应用程序音量 slider

转载 作者:可可西里 更新时间:2023-11-01 09:19:42 59 4
gpt4 key购买 nike

我有一个使用 soundplayer 播放 .wav 文件的应用程序,我查了它但找不到改变它播放的音量的方法。我正在寻找的是改变音量通过程序独立文件或有一个 slider 来改变 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;
}
}

最佳答案

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

希望对您有所帮助。

关于C# Windows 窗体应用程序音量 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331431/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com