I am working on a game application. But there is a problem on volume control. I want to change media player volume using a seekBar, and not system media volume. Is there any solution for change media player volume via seekBar.
Thanks in advance.
First, get a reference from AudioManager;
AudioManager audioMan = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
To raise the volume:
audioMan.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_VIBRATE | AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
To low the volume:
audioMan.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_VIBRATE | AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
To get the max volume:
int maxVolume = audioMan.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
To get the current volume:
int currVolume = audioMan.getStreamVolume(AudioManager.STREAM_MUSIC);
You can remove any of the flags that I've included
May be this help...
protected static void setVolume(int volume) {
currentVolume = volume;
{
if (volume == 1) {
volume = 2;
}
try {
float vol = ((float) volume / CONSTANT.SYSTEM_MAX_VOLUME);
mediaPlayer.setVolume(vol, vol);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Related
I`m trying to make an android app that can control Bluetooth volume.
till now i managed to change Bluetooth volume value for calls. using this piece of code :
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = audioManager.getStreamMaxVolume(6);
int lVol = ((volumeValue / 100) * streamMaxVolume);
audioManager.setStreamVolume(6, lVol, 0);
now the problem that I have is I cannot find the int value for streamType which is allocated for Bluetooth Media volume.
well I found the answer.
looks like after a bluetooth device connect to the phone. in order to change the bluetooth media volume the app should wait for some amount of time. I`ve set my method to run its codes after 10sec. here it is :
public void strat(BtDevice btDevice){
new Timer().schedule(
new TimerTask() {
#Override
public void run() {
AudioManager audioManager = (AudioManager) myContex.getSystemService(Context.AUDIO_SERVICE);
int volumeValue = btDevice.getMusicVolume();
int streamMaxVolume = audioManager.getStreamMaxVolume(3);
int lVol = ((volumeValue / 100) * streamMaxVolume);
audioManager.setStreamVolume(3, lVol, AudioManager.FLAG_SHOW_UI);
}
},
10000
);
I know that code below is the max volume
mAudioManager.getStreamMaxVolume
how can i get the minimum volume?
I used this code but not the minimum volume
mAudioManager.getStreamVolume(0);
This my code
int Minvolume=myAudioManager.getStreamVolume(0);
int current=myAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int volume=(int) (current-1);
if (current>=Minvolume){
myAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
handler.postDelayed(this, 10000);
System.out.println("ADJUST LOWER");
}
else if (current!=Minvolume){
System.out.println("volume is minimum");
}
Try the following to reduce the volume of the music stream...
for (int volume = myAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) - 1; volume >= 0; volume--) {
//
// add your delay code here
//
// Call a method to set absolute volume
setVolume(volume);
}
private void setVolume(int volume) {
myAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
}
A better approach to increase or decrease volume would be to use adjustVolume.
public void decreaseStreamVolume(int volType, AudioManager am){
am.adjustStreamVolume(volType, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
}
You can enter AudioManager.STREAM_MUSIC or any desirable stream and AudioManager.ADJUST_LOWER or AudioManager.ADJUST_HIGHER as per your requirement. I believe it's much cleaner way to execute.
I want to realize the mute effect under the condition of mobile phone without root.
private AudioManager myAudioManager = null;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int ringerMode = myAudioManager.getRingerMode();
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
myAudioManager.setRingerMode(ringerMode);
I test the above method. It has the effect on some mobile. And other mobile has no effect. Why?
What other way to mute except this?
There may be version sdk issue. By the way there are several streams in Android, which one do you want to mute? Try this code:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // mute music stream
audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0); // mute ring stream
if (Build.VERSION.SDK_INT >= 8) {
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
audioManager.requestAudioFocus(null, AudioManager.STREAM_RING, AudioManager.AUDIOFOCUS_GAIN);
}
I am using the ToneGenarator in order to play a continuous beep.
The problem is that the volume is far too low!
We can't hear anything when the volume is lower than 8.
And with the maximum volume, it is not loud at all...
Is it a limitation of the DTMF sound or am I doing something wrong?
Here is the code:
private final int TONE_TYPE = ToneGenerator.TONE_DTMF_5;
private final int STREAM = AudioManager.STREAM_MUSIC;
private final int DOT_TIME = 3;
public SoundManager(Activity anActivity) {
audio = (AudioManager) anActivity
.getSystemService(Context.AUDIO_SERVICE);
generator = new ToneGenerator(STREAM,
audio.getStreamMaxVolume(STREAM));
}
private void playBeep() {
generator.startTone(TONE_TYPE, DOT_TIME);
}
Use this one, For using Current System Volume.
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
I found where the problem was:
ToneGenerator takes a volume between 0 and 100, while the stream volume is between 0 and 15.
Thus, audio.getStreamMaxVolume(STREAM) gives 15 out of 100, it is low...
I have a button that when pressed sounds a short sound effect with SoundPool. the file is a 16k mp3.
The sound effect works for hundreds of button clicks, but at some point i get this error (and the sound isn't playing for clicks anymore):
E/AudioFlinger( 34): no more track names available
E/AudioTrack( 904): AudioFlinger could not create track, status: -12
E/SoundPool( 904): Error creating AudioTrack
W/AudioFlinger( 34): write blocked for 81 msecs, 4573 delayed writes, thread
xb3f0
onCreate:
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) {
// TODO Auto-generated method stub
loaded = true;
}
});
//save the effect somewhere:
MyUtil.regularSound = soundpool.load(this, R.raw.regular_click,1);
onClick:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVol = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVol = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float vol = actualVol/maxVol;
if (loaded) {
soundpool.play(MyUtil.regularSound,vol,vol,1,0,1f);
}
before starting another intent:
Intent i = new Intent(con, EndScreen.class);
if (soundpool != null) {
soundpool.release();
soundpool = null;
}
startActivity(i);
I just spent hours with the same problem.
The -12 error means that sound allocated to the app is out of memory. using .finish() doesn't release the memory of SoundPool. You have to use .release().
In my app I have a soundPool.release() on the onclick method that starts the next activity,
Changing the sound formats didn't work for me either.
Im getting the same error. The thing I did was to try to resample the track, convert it to OGG format and change the quality to 0. I did all this with Audacity
You try code it: here
/*
private SoundPool mSoundPool;
private SparseArray<Integer> mSoundPoolMap;
private AudioManager mAudioManager;
*/
public static void clear() {
if (mSoundManager != null) {
mSoundManager.mSoundPool = null;
mSoundManager.mAudioManager = null;
mSoundManager.mSoundPoolMap = null;
}
mSoundManager = null;
}