Issue with the sound volumes in button press: - android

I'm developing currently an IME, and I have sound for button click. I have an option in the preferences screen to change the volume of the sounds. The SeekBar values are going from 0.0 to 1.0. Now I try to let the user to configure the volume of the buttons in the preferences screen and later I get this value and save it as mSoundVol parameter. So for the sound of the click I wrote the following method:
float soundVolume;
int maxVolume, sound;
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_DELETE);
sound = mAudioManager.FX_KEYPRESS_DELETE;
Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_DELETE");
break;
case ASCII_ENTER:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_RETURN);
sound = mAudioManager.FX_KEYPRESS_RETURN;
Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_RETURN");
break;
case ASCII_SPACE:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_SPACEBAR);
sound = mAudioManager.FX_KEYPRESS_SPACEBAR;
Log.d(TAG+ "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_SPACEBAR");
break;
default:
maxVolume = mAudioManager.getStreamMaxVolume(mAudioManager.FX_KEYPRESS_STANDARD);
sound = mAudioManager.FX_KEYPRESS_STANDARD;
Log.d(TAG + "-volume", "chosen sound: mAudioManager.FX_KEYPRESS_STANDARD");
}
soundVolume = maxVolume * mSoundVol;
Log.d(TAG+ "-volume", "current max volume: " + maxVolume + " current volume setting: " +mSoundVol * 100 +"%" + " volume result: " + soundVolume);
mAudioManager.playSoundEffect(sound, soundVolume);
But for some reason this does not change the volume of the sound for the user.
Can some one tell me what am I doing wrong with the AudioManager here?
Thanks.

your need to use this method for a more accurate result
AudioManager audioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
[int value],
[if desired a flag]);
EDIT
Take this FLAg as example
AudioManager.FLAG_PLAY_SOUND
This means that whenever the user press the volume Button a bip will be outputed
EDIT 2
here is an implementation of the code
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.getStreamMaxVolume(),
AudioManager.FLAG_PLAY_SOUND);

The solution in my case was this:
float soundVolume;
int maxVolume, sound;
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
sound = mAudioManager.FX_KEYPRESS_DELETE;
break;
case ASCII_ENTER:
sound = mAudioManager.FX_KEYPRESS_RETURN;
break;
case ASCII_SPACE:
sound = mAudioManager.FX_KEYPRESS_SPACEBAR;
break;
default:
sound = mAudioManager.FX_KEYPRESS_STANDARD;
break;
}
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
soundVolume = maxVolume * mSoundVol;
mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM , (int)(soundVolume * 1.5) , 0);
mAudioManager.playSoundEffect(sound);
The important thing was to pass 0 as the last parameter of the setStreamVolume method, that way this action only changes the volume without playing any other sound of showing the volume UI.

Related

Read out Alarm volume

I try to read out the Alarm volume the following way, but I always get wrong values. The ringer volume is always correct!!
That happens on every Smartphone, HTC, Samsung, Sony and even on the virtual device. What could be the problem?
private AudioManager amanager;
#Override
public void onCreate(Bundle savedInstanceState)
{
amanager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
}
int volume = 3;
int alarm = 3;
try{
volume = amanager.getStreamVolume(amanager.getStreamVolume(AudioManager.STREAM_RING));
alarm = amanager.getStreamVolume(amanager.getStreamVolume(AudioManager.STREAM_ALARM));
Log.i("!!!!!!!!!!", "Activity!!, Volume ringer: " + volume + " Vol Alarm: " + alarm);
}catch(IllegalArgumentException e){}
I was getting wrong values for the ringer volume. The method getStreamVolume (int streamType) gets an int and returns int. When you write:
alarm = amanager.getStreamVolume(amanager.getStreamVolume(AudioManager.STREAM_ALARM));
you're actually getting the volume of the alarm (lets say it is X), and then passing to alarm the value alarm = amanager.getStreamVolume(X);
so the result is unexpected.
change the values of volume and alarm to -
volume = amanager.getStreamVolume(AudioManager.STREAM_RING);
alarm = amanager.getStreamVolume(AudioManager.STREAM_ALARM);

Play a random sound on a button click

I wonder if you could help. Im trying to play a random sound (from a set of 7 available sounds) when a user clicks on a button. So far I have:
Random rand = new Random();
int rndm = rand.nextInt(6) + 1; // I have 7 random sounds to play sequentially named 'my sound' + n.
String sndName = "mysound" + rndm; // Assign a random # to the end of the sound file.
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sndName); // ERROR HERE: Expects an Int
mp1.start();
My hope was that one of my sound files (mysound1, mysound2, mysound3,...) would play randomly but eclipse complains that the mp1 assignment is expecting an int. Any ideas?
Thanks much.
Use
int[] sounds={R.raw.mysound1, R.raw.mysound2, R.raw.mysound3,R.raw.mysound4,R.raw.mysound5,R.raw.mysound6,R.raw.mysound7};
Then
Random r = new Random();
int Low = 0;
int High = 7;
int rndm = r.nextInt(High-Low) + Low;
mp1 = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
mp1.start();
The issue is that you need to get the resource id to pass in. You'll need to use something like this.
int id = getResources().getIdentifier("mysound" + rndm, "raw", getApplicationInfo().packageName);
mp1 = MediaPlayer.create(getApplicationContext(), id);
Two things. First I recommend you look at the line above where you create the string. If I remember correctly you may need to use a function such as to String to get the integer to be a string to concatenate on your string.
Next, I don't believe the r.raw is going to work like that.
My recommendation is if you have only 7 items, make an array holding the resources.
int [] resources = {R.raw.mysound1, R.raw.mysound2} etc
Then use the index from before to select the right R file.
So in the media create line, replace R.raw.sndName with resources[rndm]
your program using variable error. see below code:
// 1. R.raw.sndName is Constant
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sndName);
// 2. android resource must integer, Starting from this idea

Use String in SoundPool play

I'm using SoundPool for my android app. I load about 75 one- to three-second sounds into the pool in my main activity and then reference them from other activities using the Sound method which looks like this:
public void Sound(int s){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
MainActivity.spool.play(s, volume, volume, 1, 0, 1f);
};
s is the integer I define in the MainActivity class, for example:
static int sound_e;
and then pass into the Sound method like this:
Sound(sound_e);
I would like to be able to define a string like this:
String letter_sound = "MainActivity.sound_" + currentLetter;
//Example value of this string would be MainActivity.sound_a
And then pass that string into Sound as an integer. This is to avoid 26 if statements, which I did for the numbers like this:
if (randomNum == 1) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_1);}
else if (randomNum == 2) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_2);}
else if (randomNum == 3) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_3);}
else if (randomNum == 4) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_4);}
else if (randomNum == 5) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_5);}
else if (randomNum == 6) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_6);}
else if (randomNum == 7) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_7);}
else if (randomNum == 8) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_8);}
else if (randomNum == 9) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_9);}
else if (randomNum == 10) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_10);}
else Log.v(TAG, "None of the ifs are true. randomNum: " + randomNum);
I didn't see in the android documentation any way to pass string values into the SoundPool play, just integers. Thanks for helping me figure out what I'm missing!
I'm using SoundPool for my android app. I load about 75 one- to three-second sounds into the pool in my main activity and then reference them from other activities using the Sound method...
Firstly...DO NOT DO THIS. The Android Activity class is not just a normal Java class and the proper (and safe) Android programming approach is that an Activity should be self-contained. Other Activities or app components should not have access to public methods or data fields.
If you wan't to provide a common class to be used by multiple app components then simply create a 'helper' POJO class. If it needs to use a Context (as SoundPool does for example), you can simply pass the Activity context into methods as this. Be careful about holding a reference to the Activity Context however as it can cause memory leaks.
Secondly to give an easy answer to your problem...
The load(...) methods of SoundPool return a soundID which is what you use to call the play(...) method. If all you want to do is play a random sound then simply create an array of some sort and use the random number to get the soundID from the array by index. Simple - just one line of code to access the relevant soundID.

Get random sound file from raw folder - Android - MediaPlayer

I'd like to:
Generate a random number (easy enough)
take that random number and add it to some sort of dynamic path
use the dynamic path in the mediaplayer creation.
E.g.
Resource folder(raw) contains
1.mp3
2.mp3
3.mp3
Random number generates a number between 0 and 4
e.g: 2
Inserts random number to path
E.g. a String?
String PATH = "R.raw." + RANDOM-NUMBER
MediaPlayer mp = MediaPlayer.create(context, PATH);
However, of course MediaPlayer uses a URI variable not a string, i did try
myUri = Uri.parse("R.raw.2");
but get a nullPointerException
I imagine this is very simple/straight forward to answer and my knowledge simply evades me
Thanks very much
You cant do that with resources. Here's how I see you'd go about it.
int randomInt = generateRandomInt(), id;
switch (randomInt) {
case 1:
id = R.raw.1;
break;
case 2:
id = R.raw.2;
break;
...
}
setDataFromResource(getResources(), myMediaPlayer, id);
AFAIK, you have to use setDataFromResource to set the data for your media player from R.raw
Personally I would do it like this:
//initializing the sounds
final MediaPlayer sound1 = MediaPlayer.create(this, R.raw.1);
final MediaPlayer sound2 = MediaPlayer.create(this, R.raw.2);
final MediaPlayer sound3 = MediaPlayer.create(this, R.raw.3);
//generate random number
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(3) + 1;
//picking the right sound to play
switch (randomInt){
case 1: sound1.start();
break;
case 2: sound2.start();
break;
case 3: sound3.start();
break;
}

android.media.AudioManager.setStreamVolume(int streamType, int index, int flags)

I am trying to change volume of notifications, but not ringer.
However, when I use this function notification and ringer volume are changed.
Example code:
AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int volNot = mAudioManager.getStreamVolume(STREAM_NOTIFICATION);
int volRing = mAudioManager.getStreamVolume(STREAM_RING);
Log.d(volNot + "," + volRing, "not, ringer");
mAudioManager.setStreamVolume(STREAM_NOTIFICATION, 1, 0);
volNot = mAudioManager.getStreamVolume(STREAM_NOTIFICATION);
volRing = mAudioManager.getStreamVolume(STREAM_RING);
Log.d(volNot + "," + volRing, "not, ringer");
For result setStreamVolume(STREAM_NOTIFICATION, 1, 0) changes value of notifications(STREAM_NOTIFICATION) to 1, but it also changes value of ringer(STREAM_RING) to 1.
This is because the phone has a setting in sounds---> volume in which there is a tick box which is checked, uncheck it and try your code again, this setting says that the ringer volume will be your notification volume and visa-verse.
Since ICS (Android 4.0), Ringer and Notification shares volume stream in official Android ROMS. Before this, user could choose to split the streams.

Categories

Resources