How to set Soundmanager play volume at ringtone volume? - android

I am trying to play SoundManager using the following, but it still seams to be using the media volume instead of the ringtone volume. Why?
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume,streamVolume, 1, 0, speed);

As per Michael, I have to change this in "initSounds" in my SoundManager also. Thanks Michael.

Related

android sound pool not appear

I want to add a sound effect when a user clicks on a button.
I tried this:
case R.id.b_all_addresses_addAddress:
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
if (loaded)
soundPool.play(soundID, volume, volume, 1, 1, 1f);
else
Toast.makeText(this, "could I hate you God more?", Toast.LENGTH_SHORT).show();
in my on create I did this:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.thip, 1);
the sound is just 6 KB.
My Problem
I can't hear the voice
what values are you getting back for volume?
They should be between 0.1 and 0.99. Try using 0.5 as a hard-coded setting and see whether that works. I vaguely remember someone here once reported a "bug" in SoundPool which meant that 0 and 1 meant "mute" and your volume had to be between these two values in order to be heard.
Also: What format is your sound in? not all phones can play all the formats. Some need ogg, some are fine with WAV.
This here works for me on HTC Sensation XE, Android 4.03:
*WAV
*2 Channels
*16 bit PCM
*44.1kHz

Set volume to max in Android [duplicate]

This question already has answers here:
Android: how to play music at maximum possible volume?
(5 answers)
Closed 9 years ago.
In my app, I am trying to set the volume when playing an audio clip to the maximum level but it doesn't appear to have any affect. I have to manually adjust the volume to the maximum level. Here's my code:
MediaPlayer mp = new MediaPlayer();
mp.setVolume(1, 1);
AudioManager
int origionalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
Use This
it s for total volume
AudioManager mgr = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE);
int valuess = 9;//range(0-15)
mgr.setStreamVolume(AudioManager.STREAM_MUSIC, valuess, 0);
it is for left right while current song is playing...
AudioTrack m = (AudioTrack) appContext.getSystemService(Context.AUDIO_SERVICE);
m.setStereoVolume(leftVolume, rightVolume);
it works for me.
Using AudioManager you can control the volume of media player.
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
also from MediaPlayer
public void setVolume (float leftVolume, float rightVolume)
for example you can use this method as,
int maxVolume = 100;
float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
mp.setVolume(1-log1);

android : when using MediaPlayer to play background music, sound effects are stopped prematurely

I'm trying to play background music on my app and occasional sound effects when you kill an enemy and stuff like that.
All the sound effects worked, but then I started using a MusicManager class (similar to this tutorial: http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion) to try to play background music and it works, but the sound effects get chopped after half a second or so.
I'm playing the sound effects using:
MediaPlayer mp = MediaPlayer.create(context, R.raw.fire);
mp.start();
Use soundpool instead.
SoundPool can play multiple streams at once at different volumes, speeds, and looping.
MediaPLayer isn't really meant to handle game audio.
I have 2 games published in the market, and both use SoundPool and have no issues.
here these two functions are taken right out of my game.
public static void playSound(int index, float speed)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
}
public static void playLoop(int index, float speed)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / 3f;
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, speed);
}
that is how easy it is. To take a closer look at this, I only use my playLoop() to play background music, so I lower the volume on it, but you could easily modify the code to manually set the volume each time you play.
also
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, speed);
the first argument mSoundPoolMap.get(index) is just a container holding all of my sounds. I assign each sound a final number such as
final static int SOUND_FIRE = 0, SOUND_DEATH = 1, SOUND_OUCH = 2;
I load thos sounds into those positions and the play them from it. (remember you dont want to be loading all your sounds every time you run one, just load them once.) The next 2 arguments are left/right volume, priority, and then -1 to set to loop.
mSoundPool = new SoundPool(8, AudioManager.STREAM_MUSIC, 0);
this sets my soundpool to 8 streams. the other is the source type and then the quality.
have fun!

Setting the Initial Volume to the phones ring volume

im trying to make it so when the user opens the app it sets the volume of the music to whatever they have their phones ringer volume at. This is my code so far but im not exactly sure what the paramters on setVolume(float, float) are. The android documentation doesn't explain it well. What is my code doing wrong here?
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
mPlayer = MediaPlayer.create(this, R.raw.song);
mPlayer.setOnErrorListener(this);
if(mPlayer!= null)
{
mPlayer.setLooping(true);
mPlayer.setVolume(currentVolume,1);
}
Looks like audio.setStreamVolume is what you want, but pass in STREAM_MUSIC instead of STREAM_RING.
Note: the music volume and ringer volume are likely to have different maximum values, so you will need to normalize them. Use getStreamMaxVolume for that.
I have not done this before, and I haven't compiled this, but the code should look something like this
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Get the current ringer volume as a percentage of the max ringer volume.
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int maxRingerVolume = audio.getStreamMaxVolume(AudioManager.STREAM_RING);
double proportion = currentVolume/(double)maxRingerVolume;
// Calculate a desired music volume as that same percentage of the max music volume.
int maxMusicVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int desiredMusicVolume = (int)(proportion * maxMusicVolume);
// Set the music stream volume.
audio.setStreamVolume(AudioManager.STREAM_MUSIC, desiredMusicVolume, 0 /*flags*/);

Android SoundPool.stop does not seem to work

I have created a class MySoundPool (I am using this class as sigelton, but don't think this is relevant as everyting else works). I am initianalizing SoundPool, a HashMap, and get the context for AudioManager. Thereafter I am loading two sounds.
MySoundpool is used by method MySoundPool.playSound(int index, float rate)
playSound clips rate to 0.5 <= rate >= 2.0 an executes statements
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, index, 0, rate);
So far so good. Everything works fine.
No it happens that playSound is called while the previous sound still plays, and I want to stop that before playing the new sound. Prior to the above code snippet I tried
mSoundPool.stop(mSoundPoolMap.get(index));
and
mSoundPool.autoPause();
with no success. The sound just continues to play to its end.
Any comments will be appreciated
I assume that you create a new SoundPool object in the constructor of your MySoundPool class?
If so then the first argument that SoundPool's constructor takes is the number of streams to allow at the same time. for example...
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
That will allow 10 sounds to play at once so just change the 10 to a 1 and that should do it.
Edit:
The stop() method takes a stream id as an argument, which should be the number returned from the play() method. You could try setting a variable equal to what play() returns and then use that variable when stopping the sound.
Use the soundId to play a sound but use the streamId to stop it. These are not always the same number!
When you start the sound, store the returned streamId:
int myStreamId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, index, 0, rate);
Then use that streamId (not the soundId) to stop the sound:
mSoundPool.stop(myStreamId);
As it says in the class document:
public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
Since: API Level 1
Play a sound from a sound ID. Play the sound specified by the soundID. This is the value returned by the load() function. Returns a non-zero streamID if successful, zero if it fails. The streamID can be used to further control playback.
Returns
non-zero streamID if successful, zero if failed
Had the same problem, why does one not read the F M ! :D
-Thanks guys!

Categories

Resources