开发者

Change The Volume in System.Media.SoundPlayer

开发者 https://www.devze.com 2022-12-18 17:20 出处:网络
I am using Syste开发者_JS百科m.Media.SoundPlayer to play some wav files in my project. Is it possible to change the volume of this SoundPlayer?If there is no way to do that, how can I change the volum

I am using Syste开发者_JS百科m.Media.SoundPlayer to play some wav files in my project. Is it possible to change the volume of this SoundPlayer? If there is no way to do that, how can I change the volume of my computer using C#?


From SoundPlayer adjustable volume:


Unfortunately SoundPlayer doesn't provide an API for changing the volume. You could use the MediaPlayer class:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消