How to get and set the sound volume level for Universal Windows application

The title says everything, I would like to get and set the sound volume level for the Universal Windows (iot) application.

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

I am using visual studio 2015

I was looking for examples / samples, but came up with a dry one.

Any help would be appreciated.

(note that this is not a duplicate question - I want to get and set the sound volume level for the Universal Windows application - this cannot be done using the COM object in uwp - and not use the VBScript or JScript uwp applications)

+5
source share
5 answers

Take a look at the source code: https://github.com/File-New-Project/EarTrumpet

It will contain what you are looking for.

0
source

You can control the sound volume for your own application using the Volume Media Media property. It does not control the total system volume, but allows you to set your own application volume.

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

For example, I have a view model in which I expose the following property and to which the property of the slider property is bound. (My Min slider is 0, and its Max is 100, so I convert the 0-1 value of the MediaPlayer volume property to the range 0-100.)

(NOTE: I also have a single “BackgroundAudioService” to wrap the link to the background sound of MediaPlayer, as the background audio project of the UWP sample on github demonstrates, but it's just a link to 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 does not work, try checking and installing the system volume, since this method only controls the volume of your application.

0
source

I searched the same, and I found what I was looking for. So, I felt obligated to publish (and share) my results here.

To control the entire system, you can do this using the Sound Global API .

 ElementSoundPlayer.Volume = 0.5f; 

All sounds in the application can be dimmed with the volume control. However, the sounds in the application cannot become louder than the system volume.

0
source

In fact, this is documented.

You can set or set the volume of an audio device in the same way as in desktop applications using IAudioEndpointVolume , the documentation states that it is supported in Windows Store applications.

To activate this interface for a specific device in a UWP application, 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, but for the IAudioEndpointVolume interface.

Finally, at the moment, this API is only available in C ++. if you want to do this from a C # or VB UWP application, you can create a RuntimeComponent in C ++ CX that will provide this function to your application.

0
source

There is no need to create a WinRT component. Instead, you can use P / Invoke to access win32 audio interfaces.

Check out my Gist code on how to do this in C #.

0
source

Source: https://habr.com/ru/post/1239647/


All Articles