Volume change in System.Media.SoundPlayer

I am using System.Media.SoundPlayer to play some wav files in my project. Can I change the volume of this SoundPlayer? If there is no way to do this, how can I change the size of my computer using C #?

+5
source share
1 answer

From SoundPlayer volume is adjusted :


Unfortunately, it does not provide an API for changing volume. You can use the class : SoundPlayer 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 also need to add links to assemblies PresentationCoreand WindowsBase.

+1
source

All Articles