OpenSL/ES : maximum volume of AudioPlayer

2,697 views
Skip to first unread message

mootoh

unread,
Jul 28, 2011, 9:35:37 PM7/28/11
to andro...@googlegroups.com
From the document in r6 (docs/opensles/chart1.png), it looks we can get/set the volume of AudioPlayer object via VolumeItf.
The result is however, the return value of GetMaxVolumeLevel() is always 0, thus I canot set the volume anything but 0.
Am I missing something?

I tweaked the sample code (samples/native-audio) to get/set the volume like:

    const SLInterfaceID ids[2] = {SL_IID_SEEK, SL_IID_VOLUME};
    const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};

    (*engineEngine)->CreateAudioPlayer(engineEngine, &fdPlayerObject, &audioSrc, &audioSnk, 2, ids, req);
    (*fdPlayerObject)->Realize(fdPlayerObject, SL_BOOLEAN_FALSE);

    SLVolumeItf volumeIf;
    (*fdPlayerObject)->GetInterface(fdPlayerObject, SL_IID_VOLUME, &volumeIf);

    SLmillibel maxVolume;
    (*volumeIf_)->GetMaxVolumeLevel(volumeIf, &maxVolume);

maxVolume => 0


I'm on NDK r6, Nexus S, OS 2.3.4.

Thanks in advance,

mic _

unread,
Jul 29, 2011, 6:57:01 AM7/29/11
to andro...@googlegroups.com
On Android the max level is 0 (unity gain). You can go below that (negative values), but you can't go above 0.

/Michael

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/cX4DlZuX5vwJ.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

mootoh

unread,
Jul 29, 2011, 6:50:06 PM7/29/11
to andro...@googlegroups.com
Thanks mic, it works!

Crazii

unread,
Jun 18, 2014, 1:52:16 AM6/18/14
to andro...@googlegroups.com
I'm working on NDK now, and have this problem.
Here's my solution, Just in case anyone encounter this problem:

according to the doc, the volume ranges from [[SL_MILLIBEL_MIN, GetMaxVolumeLevel()].
so we can use a normalized scalar [0,1] to set the volume.

float nvolume = 0.5;

//get min & max
SLmillibel MinVolume = SL_MILLIBEL_MIN;
SLmillibel MaxVolume = SL_MILLIBEL_MIN;
(*SLES_Volume)->GetMaxVolumeLevel(SLES_Volume, &MaxVolume);

//calc SLES volume
SLmillibel Volume = MinVolume + (SLmillibel)( ((float)(MaxVolume - MinVolume))*nvolume ) );

//set
(*SLES_Volume)->SetVolumeLevel(SLES_Volume, Volume);

Crazii

unread,
Sep 3, 2014, 9:34:52 PM9/3/14
to andro...@googlegroups.com
It seems that the dB is not linear
"The definitions of the decibel and bel use base 10 logarithms."  (http://en.wikipedia.org/wiki/Decibel)


Now I use the formula: int dBVolume = 20* log2(x)/log2(10);
SLmillibel volume = dBVolume*100; //1dB = 100mB
Reply all
Reply to author
Forward
0 new messages