I want to make a Seek Bar that controls DTMF volume(e.g 0 to 100). I have searched a lot but could not find any thing. I am doing this but its not working..
int seekbarValue=seekBar.getProgress();
AudioManager audioManager=(AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_DTMF, seekbarValue, 0);
Please any one tell me a solution to control DTMF volume.
None of the (AudioManager.STREAM_*) volumes go to 100 (int).
A valid stream volume for setStreamVolume(...) is between 0 and getStreamMaxVolume(int streamType).
Each stream can have a different max volume int, like 8, 10, or 16 from what I remember. Might even be different on different devices.
I hope that is enough to point you and future visitors in the right direction.
I wanted to add a little more information to the answer of #Anonsage.
As #Anonsage mentioned, each stream has a different max volume. As per Kitkat 4.4.2 implementation, these are the max values.
STREAM.DTMF: 15
STREAM.MUSIC: 15
STREAM.VOICECALL: 5
STREAM.RINGTONE: 7
If you actually get into the lower levels and look into the AudioService.java of AOSP code, there is a rescaling operation that helps to show similar UI and update the actual values of the stream.
Related
I imagine the answer to this question must be something quite typical. The point is that I have some buttons that play sounds when you click on them. I want to control the sound volume. How do I control the sound volume? I am using the following code:
private fun playSound() {
val sound = getSystemService(Context.AUDIO_SERVICE) as AudioManager
sound.playSoundEffect(AudioManager.FX_KEY_CLICK, 1f)
}
https://developer.android.com/reference/android/media/AudioManager#playSoundEffect(int,%20float)
volume float: Sound effect volume. The volume value is a raw scalar so UI controls should be scaled logarithmically. If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used. NOTE: This version is for applications that have their own settings panel for enabling and controlling volume.
So you need to adjust that second parameter. I haven't used it, but it says that's a scalar so I'd imagine 1 is full volume? It doesn't mention any other value as a max constant or anything. -1 gives you a sound a little quieter than the current media volume setting, if that's convenient. You'll need to play around with different values between 0 and 1 (I assume!) and see what you need
My question is simple: what is the default number of the bands provided by the built-in android equalizer? Also, what is the guaranteed minimum number of bands?
As far as I researched, the answer appears to be 5, but it is not very well documented. However, testing it on my devices, which is currently available, I got the following result:
HTC Desire S running android 2.3.5: 5 bands
Sony Xperia Tipo running android 4.0.x: 5 bands
however, Nexus 4 running Android 4.3.1: 6 bands
The way I get theese numbers is the following:
MediaPlayer mp=new MediaPlayer(this);
/* some initialization */
Equalizer eq=new Equalizer(0, mp.getAudioSessionId());
short bands=eq.getNumberOfBands();
So, on some devices, I may be able to get more bands, but the minimum number is 5?
Also, is that a good approach that I render the UI part of the equalizer dynamically, depending on how much bands the current device has, and then let the user set his own preferences?
Thanks in advance!
I can't tell the number of bands of device. It is hardware dependent. Samsung galaxy have 13 equalizer bands and some devices have greater than it.
You can simply create any number of bands programamtically.
Equalizer eq=new Equalizer(0, mp.getAudioSessionId());
short bands=eq.getNumberOfBands();
LinearLayout parentViewToAllEqualizerSeekBars = findViewById...
for(int i=0;i<(int)bands.length;i++)
{
//create seekbar programmatically and set it min max and add to the view
Seekbar seek = new SeekBar(this);
seek.min(equalizerMinLevel....)
seek.max(equalizerMaxLevel..)
parentViewToAllEqualizerSeekBars .addView(seek);
}
Now it will work on all devices.Whether it has band less than 5 or greater than 13.
Note:
Also check whether equalizer!=null must
I do not think there is a default number of bands, and you should not build your application assuming there is a default/fixed number of bands.
Definitely you will have to render your UI equalizer dynamically, based on device number of bands.
Because of low reputation i have to tell it to you here
Maximum number of bands are 8, I have created 8 seekbar and only show seekbar ==numberOfBands
How to implement Equalizer in android
I have been trying to capture audio, within a native linux program running on an Android device via adb shell.
Since I seemed to be getting only (very quiet) noise, i.e. no actual signal (interestingly, an Android/Java program doing similar did show there was a signal on that input),
I executed alsa_amixer, which had one entry that looked like the right one:
Simple mixer control 'Capture',0
Capabilities: cvolume cswitch penum
Capture channels: Front Left - Front Right
Limits: Capture 0 - 63
Front Left: Capture 31 [49%] [0.00dB] [off]
Front Right: Capture 31 [49%] [0.00dB] [off]
"off". That would explain the noise.
So I looked for examples of how to use alsa_amixer to unmute the channels, I found different suggestions for parameters like "49% on" or "49% unmute", or just "unmute" none of which works. (if the volume% is left out, it says "Invalid command!", otherwise, the volume is set, but the on/unmute is ignored)
I also searched how to do this programatically (which I'll ultimately need to do, although the manual approach would be helpful for now), but wasn't too lucky there.
The only ALSA lib command I found which sounds like it could do something like that was "snd_mixer_selem_set_capture_switch_all", but the docs don't day what the parameter does (1/0 is not on/off, I tried that ;) )
The manual approach to set these things via alsa_amixer does work - but only if android is built with the 'BoardConfigCommon.mk' modified, at the entry: BOARD_USES_ALSA_AUDIO := false, instead of true.
Yeah, this will probably disable ALSA for android, which is why it wouldn't meddle with the mixer settings anymore.
To you android programmers out there, note that this is a very niche use case of course, as was to be expected by my original post to begin with.
This is not what most people would want to do.
I just happen to tinker with an android device here in unusual ways ;-)
Just posting the code as question giver suggested, also don't like external links.
#include <alsa/asoundlib.h>
int main()
{
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, "default");
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, "Capture");
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
snd_mixer_selem_set_capture_switch_all(elem, 0);
snd_mixer_selem_set_capture_dB_all(elem, 0, 0);
snd_mixer_close(handle);
}
In order for the user to control the volume , my android application has a menu consisting of a slider that provides int values from 0 to 10 , when dragged. After I obtain a value , I must set the volume to the corresponding value chosen by the user , and well , this is the part that I don't know to implement and I 'd like to find about it.
Use the AudioManager class. Essentially the code goes as follows:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(streamType, volume, flags);
The problem is that the volume of the device isn't necessarily mapped from 0 to 10 as you have in your slider. On my emulator, it's from 0 to 7. So what you need to do is getStreamMaxVolume(...) to know what your max is, and then work out your value as a fraction of that. As an example, if your user chooses volume 8 out of 10, that's equivalent to 0.8 * 7 = 5.6, which you should round to 6 out of 7.
The "stream" refers to things like ringer volume, notification volume, music volume, etc. If you want to change the volume of the ringer, you need to make sure all your commands have AudioManager.STREAM_RING as the streamType.
Android's soundpool.play [documentation][1] says "The playback rate allows the application to vary the playback rate (pitch) of the sound. A value of 1.0 means play back at the original frequency. A value of 2.0 means play back twice as fast, and a value of 0.5 means playback at half speed.".
However, when I set the rate to 1.49f, I hear silence. 1.485f renders correctly (it's ogg file). Is this specific to my handset, is the documentation wrong, or am I being foolish in some other way?
[1]: http://developer.android.com/reference/android/media/SoundPool.html#play(int, float, float, int, int, float)
I'm sort of new at this, but I'm pretty sure it needs to be 0.5f, 1f, or 1.5f and no other values. It's not specific to any particular ogg file. I do know it changes the pitch during playback in soundmanager. Since no one else answered, I figured my answer might be better than no answer.