Essentials: Read Volume Level

Created on 20 Jun 2018  路  24Comments  路  Source: xamarin/Essentials

Hi,

Please add an option in Xamarin Essentials to adjust the device volume

Thanks,
Jassim

VS bug #735688

feature-request needs-more-discussion proposal

Most helpful comment

Right I don't think we'd ever allow _setting_ of the volume. I think there _might_ be a use case for reading the current volume.

All 24 comments

I do not think that the api is similar enough to warant inclusion in the Essentials Project.
Android has Android.Media.AudioManager.AdjustVolume(..)
iOS does not seem to expose this functionality except via either the avplayer instance or their volume slider control
On UWP I am clueless, though a brief google search revealed that Volume can not be changed globally but only for application sounds (I could be very wrong here).
At glance though this seems too diverse for this Project.

@Mrnikbobjeff well, this applies to Vibrate I guess if I am going to agree with you but I don't because sound is a capability too and we are not doing any media here but just checking if sound is on or off and also turning it on and off.

@jrahma Adjusting volume and getting volume/silent mode state are two different things. Consider adjusting the issue to include the API surface which is desired, that would clarify many things left undefined in your original issue.

Yeah we might be able to inquire about the current set volume, but I don't believe adjusting the volume is something that's practical to do in a cross platform way at this point due to inconsistencies (as a user, I don't want most apps controlling my volume which is why I suspect iOS does not allow this).

detecting the volume through Xamarin Essentials is also fine.

Would love for someone to do the research on how this works across the various platforms (consistencies/differences) and to propose an API for retrieving this.

In android, we can adjust the volume and touch mode. You can view the documents [here] (https://developer.xamarin.com/api/type/Android.Media.AudioManager/)

var test = (AudioManager) MainActivity.GetSystemService (Context.AudioService);

test.RingerMode = RingerMode.Silent;

In iOS and UWP I found ways to change media volume. There is a CrossMediaPlugin, we can use it as an example: https://blog.xamarin.com/play-audio-and-video-with-the-mediamanager-plugin-for-xamarin/

I think we could consider adding:

partial class DeviceInfo {
  double CurrentVolumePercent { get; }
}

I'd like to see evidence that this can actually be done on iOS, Android, and UWP before we go ahead and mark it ready for implementation.

@Redth , I'm confused, is the API is to control the volume of the device or volume for the application?

My assumption was for the device (not sure the platforms all support application volume.

Just thinking about this API... changing the volume. Why would a developer want to do this? This is a very personal thing for an app to do. I am doing something and then the app decides it is time to go loud? Or softer? And, how hard is it to hit the volume rocker?

I think the needs a specific use case as I personally hate it when some app touches my device volume. And, my android phone has 3 volumes - music, alert, phone. What will it change?

This needs a specific use case.

Right I don't think we'd ever allow _setting_ of the volume. I think there _might_ be a use case for reading the current volume.

And maybe some event for volume changed. An app may want to display some volume overlay when it is changed - sort of like when you change the volume on iOS when playing a movie.

UWP Integration point
iOS Integration point
Android Integration point

IAudioEndpointVolume is supported on uwp, though I thoroughly dislike com interop.

My api proposal would be:

public static class Volume
{
float MusicVolume {get; }
}
where MusicVolume ranges from 0 to 1 on all platforms.

After further research this could be extended by a VolumeChangedEvent which fires on music volume changes for android, always on uwp global volume change and on ios global volume change.

The thing that still bugs me about this implementation is Android and the wonky way it deals with volume.

Is the music volume generally what controls playing sounds in apps? Is there a way on Android to play sounds that aren't controlled by the music volume?

@Redth , in android we have two volume types, the Media, for streaming and Alarms for ringtone. Below are some code snippets to deal with.

am = (AudioManager)CurrentActivity.GetSystemService(Context.AudioService);
am.RingerMode = RingerMode.Vibrate;
//  RingerMode.Vibrate ; RingerMode.Silent ; RingerMode.Normal

or you can more general

  static void PlataformIncreaseVolume(int volume)
        {
            am = (AudioManager)CurrentActivity.GetSystemService(Context.AudioService);
            am.SetStreamVolume(Stream.Alarm, volume, VolumeNotificationFlags.ShowUi);          
        }

In am.SetStreamVolume(...) you use the Stream Enum to specify the media type.

Reference : https://developer.xamarin.com/api/member/Android.Media.AudioManager.SetStreamVolume/p/Android.Media.Stream/System.Int32/Android.Media.VolumeNotificationFlags/

Yep I definitely understand there are multiple volumes on Android, I guess this goes back to trying to decide what the expected use cases of this API are.

Why would I want to know the volume level in my app? To detect if it's really low and prompt the user to turn it up if the app needs to play back something?

From an android perspective which stream types would I be interested in the volume for?

A few use cases I could imagine which I saw in the wild were ads which stopped playing if the playback volume was mute. Not that this is something I like or want to have in apps, but that is something I saw. For some riddle games they hade a note saying you should increase audio volume for this level as there is an audio clue. The only stream type I aminterested in would be music. The api difference is too great to otherwise be encapsulated by the audio api - a separate ringtone volume detection could always be discussed in the future.

So do most ads, and apps generally play media that is controlled by the music stream's volume?

I am afraid your question is ambiguously worded. Was your question whether ads and apps play media which is controlled by the music stream volume? Or was the first part a statement and the second a question/statement?

I'm just trying to determine which stream(s) developers consuming this API are truly going to be interested in. Is Music enough?

You mentioned Ads might want to use this, which is why I ask, does most audio from Ads for example play over the music stream? I'm trying to get a sense of what are the most common types of audio played, and which stream are they usually played over?

We are closing this issue since no response was received. Please open a new issue referring to this one if you have more information.

@Redth Virtually all audio is played over the Music stream - I have not encountered a cross platform media app which used any other stream than Music. I think this still fits the 80/20 rule you have for Essentials. If you specifically want to play something over a stream only available on one platform you should do so yourself. Music volume is so essential that it really should be the only stream we play attention to.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ykoorb picture ykoorb  路  3Comments

Tommigun1980 picture Tommigun1980  路  3Comments

jpmock picture jpmock  路  4Comments

craylward picture craylward  路  4Comments

gabry90 picture gabry90  路  3Comments