简体   繁体   English

更改System.Media.SoundPlayer中的卷

[英]Change The Volume in System.Media.SoundPlayer

I am using System.Media.SoundPlayer to play some wav files in my project. 我正在使用System.Media.SoundPlayer在我的项目中播放一些wav文件。 Is it possible to change the volume of this SoundPlayer? 是否可以更改此SoundPlayer的音量? If there is no way to do that, how can I change the volume of my computer using C#? 如果无法做到这一点,我如何使用C#更改计算机的音量?

From SoundPlayer adjustable volume : SoundPlayer可调节音量


Unfortunately SoundPlayer doesn't provide an API for changing the volume. 不幸的是, SoundPlayer不提供用于更改音量的API。 You could use the MediaPlayer class: 您可以使用MediaPlayer类:

using System.Windows.Media;

public class Sound
{
    private MediaPlayer m_mediaPlayer;

    public void Play(string filename)
    {
        m_mediaPlayer = new MediaPlayer();
        m_mediaPlayer.Open(new Uri(filename));
        m_mediaPlayer.Play();
    }

    // `volume` is assumed to be between 0 and 100.
    public void SetVolume(int volume)
    {
        // MediaPlayer volume is a float value between 0 and 1.
        m_mediaPlayer.Volume = volume / 100.0f;
    }
}

You'll also need to add references to the PresentationCore and WindowsBase assemblies. 您还需要添加对PresentationCoreWindowsBase程序集的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM