7

The title says it all, I would like to get and set audio volume level for a Universal Windows (iot) application.

It can be either the sound level associated with my particular application - which would be ideal; or it can be the system sound level - if necessary / possible.

I am using visual studio 2015

I've been searching for examples / samples but have come up dry.

Any help would be appreciated.

(of note this is not a duplicate question - I want to get and set audio volume level for a Universal Windows application - this can not be done via a COM object in uwp - nor do uwp apps use VBScript or JScript)

Rob
  • 3,488
  • 3
  • 32
  • 27
  • it isn't possible to access arbitrary COM objects from a Universal Windows app - ref: http://stackoverflow.com/questions/32042324/how-to-create-a-com-object-in-a-uwp-application-c – Rob Jan 02 '16 at 13:40
  • @Rob-The answer you linked is correct. – WiredPrairie Jan 02 '16 at 15:52
  • @Codexer - UWP apps don't use VBScript or JScript. – WiredPrairie Jan 02 '16 at 15:52
  • Usually (at least that is how I always find the solution) is in a C++ code that is not available in C#, like for instance the system fonts, system type (7'' or more), tomorrow I can take a look if there is something in that way – Juan Pablo Garcia Coello Jan 03 '16 at 19:57

5 Answers5

1

There's no need to make a WinRT component. Instead you can use P/Invoke to access the win32 audio interfaces.

Take a look at my code (Gist) for how to do this in C#.

Wim Bokkers
  • 337
  • 2
  • 8
0

Look at the source code of this: https://github.com/File-New-Project/EarTrumpet

It'll contain what you're looking for.

Tamás Deme
  • 2,194
  • 2
  • 13
  • 31
  • 1
    I tried delving deep into this but couldn't come up with the needed code - when I got down deep I found code that matches what naudio does; but I can't get naudio to work on the windows iot platform either - doesn't want to enumerate the sound devices. I can't believe something so simple as changing the audio volume is so hard to work out. – Rob Jan 08 '16 at 02:18
0

You can control the audio volume for your own application with the MediaPlayer Volume property. It doesn't control the overall system volume but it does allow you to set your own applications volume.

https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.playback.mediaplayer.volume.aspx

For example, I have a view model where I expose the following property and to which I have a slider control's value property bound. (My slider's Min is 0 and its Max is 100 which is why I'm converting the 0-1 value of the MediaPlayer's volume property to the range of 0-100.)

(NOTE: I also have a "BackgroundAudioService" singleton to wrap the reference to the background audio MediaPlayer like the UWP sample background audio project on github demonstrates, but its just a reference to the MediaPlayer.)

    public double Volume
    {
        get
        {   
            this.volume = (BackgroundAudioService.Instance.CurrentPlayer.Volume * 100);
            return this.volume;
        }

        set
        {
            this.volume = value;
            BackgroundAudioService.Instance.CurrentPlayer.Volume = (this.volume / 100);
            this.RaisePropertyChanged();
        }
    }

If this doesn't work maybe try checking and setting the system's volume because this method only controls your app's volume.

0

I was looking for the same thing and I found what I was looking for. So, I felt obligated to post (and share) my findings here.

In order to control the entire system volume, you can do so using the Sound Global API.

ElementSoundPlayer.Volume = 0.5f;

All sounds within the app can be dimmed with the Volume control. However, sounds within the app cannot get louder than the system volume.

David Pine
  • 23,787
  • 10
  • 79
  • 107
0

Actually it is documented.

You can get or set the volume of an audio device the same way that we used to do in desktop apps using IAudioEndpointVolume, the documentation states that it is supported in Windows Store Apps.

In order to activate this interface for a specific device in a UWP app, you need to use the ActivateAudioInterfaceAsync function. There are UWP sample codes showing how to use this function to activate the IAudioClient interface. You can do the same thing but for the IAudioEndpointVolume interface.

Finally, for now, this API is only available in C++. if you want to do it from a C# or VB UWP App, you can create a RuntimeComponent in C++CX that will provide this feature to your app.

M'hand BOUGHIAS
  • 730
  • 10
  • 13