Devator
Devator

Reputation: 3904

Changing volume does move the slider, but not the audio output

I have a slider and a webbrowser object in my form and sliding it should change the volume, however it does move the slider as seen here: Slider does move, but sound output doesn't change

but it doesn't change the volume's actual output. This is probably because I integrated the WebBrowser object and using Windows 7. When I manually slide the slider (the one seen in the screenshot) the volume output does change. When playing a .wav file the volume's output does change, but not with the WebBrowser object.

I'm using the following code:

Xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Slider Minimum="0" Maximum="10" ValueChanged="ValueChanged"/>
    </Grid>
</Window>

C#

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        // Calculate the volume that's being set
        double newVolume = ushort.MaxValue * e.NewValue / 10.0;

        uint v = ((uint) newVolume) & 0xffff;
        uint vAll = v | (v << 16);

        // Set the volume
        int retVal = NativeMethods.WaveOutSetVolume(IntPtr.Zero, vAll);

        Debug.WriteLine(retVal);
    }
}

static class NativeMethods
{
    [DllImport("winmm.dll", EntryPoint = "waveOutSetVolume")]
    public static extern int WaveOutSetVolume(IntPtr hwo, uint dwVolume);

}

Upvotes: 3

Views: 4784

Answers (2)

Danny Varod
Danny Varod

Reputation: 18118

First of all this uint v = ((uint)newVolume) & 0xffff is a bit redundant, uint v = (uint)newVolume would return the same result.

Second of all, according to the MSDN documentation of the waveOutSetVolume method:

Most devices do not support the full 16 bits of volume-level control and will not use the least-significant bits of the requested volume setting. For example, if a device supports 4 bits of volume control, the values 0x4000, 0x4FFF, and 0x43BE will all be truncated to 0x4000. The waveOutGetVolume function returns the full 16-bit setting set with waveOutSetVolume.

Have you tried large changes?

Upvotes: 1

Phil
Phil

Reputation: 43011

Try using a MediaElement.

Then you could play your radio station like this:

<MediaElement LoadedBehavior="Manual" x:Name="media" />

media.Source = new Uri(@"http://shoutcastinfo.radiostaddenhaag.com/stad.wax");
media.Play();

And change the volume with

<Slider Minimum="0" Maximum="1" ValueChanged="ValueChanged"/>

private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    media.Volume = e.NewValue;
}

Both the Windows Mixer and the WPF slider now change the volume appropriately, but the values of the two are independent and don't reflect each other's' changes.

Upvotes: 2

Related Questions