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.
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 am trying to make a call recording app in Android. I am using loudspeaker to record both uplink and downlink audio. The only problem I am facing is the volume is too low. I've increased the volume of device using AudioManager to max and it can't go beyond that.
I've first used MediaRecorder, but since it had limited functions and provides compressed audio, I've tried with AudioRecorder. Still I havn't figured out how to increase the audio. I've checked on projects on Github too, but it's of no use. I've searched on stackoverflow for last two weeks, but couldn't find anything at all.
I am quite sure that it's possible, since many other apps are doing it. For instance Automatic Call recorder does that.
I understand that I have to do something with the audio buffer, but I am not quite sure what needs to be done on that. Can you guide me on that.
Update:-
I am sorry that I forgot to mention that I am already using Gain. My code is almost similar to RehearsalAssistant (in fact I derived it from there). The gain doesn't work for more than 10dB and that doesn't increase the audio volume too much. What I wanted is I should be able to listen to the audio without putting my ear on the speaker which is what lacking in my code.
I've asked a similar question on functioning of the volume/loudness at SoundDesign SE here. It mentions that the Gain and loudness is related but it doesn't set the actual loudness level. I am not sure how things work, but I am determined to get the loud volume output.
You obviously have the AudioRecord stuff running, so I skip the decision for sampleRate and inputSource. The main point is that you need to appropriately manipulate each sample of your recorded data in your recording loop to increase the volume. Like so:
int minRecBufBytes = AudioRecord.getMinBufferSize( sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT );
// ...
audioRecord = new AudioRecord( inputSource, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minRecBufBytes );
// Setup the recording buffer, size, and pointer (in this case quadruple buffering)
int recBufferByteSize = minRecBufBytes*2;
byte[] recBuffer = new byte[recBufferByteSize];
int frameByteSize = minRecBufBytes/2;
int sampleBytes = frameByteSize;
int recBufferBytePtr = 0;
audioRecord.startRecording();
// Do the following in the loop you prefer, e.g.
while ( continueRecording ) {
int reallySampledBytes = audioRecord.read( recBuffer, recBufferBytePtr, sampleBytes );
int i = 0;
while ( i < reallySampledBytes ) {
float sample = (float)( recBuffer[recBufferBytePtr+i ] & 0xFF
| recBuffer[recBufferBytePtr+i+1] << 8 );
// THIS is the point were the work is done:
// Increase level by about 6dB:
sample *= 2;
// Or increase level by 20dB:
// sample *= 10;
// Or if you prefer any dB value, then calculate the gain factor outside the loop
// float gainFactor = (float)Math.pow( 10., dB / 20. ); // dB to gain factor
// sample *= gainFactor;
// Avoid 16-bit-integer overflow when writing back the manipulated data:
if ( sample >= 32767f ) {
recBuffer[recBufferBytePtr+i ] = (byte)0xFF;
recBuffer[recBufferBytePtr+i+1] = 0x7F;
} else if ( sample <= -32768f ) {
recBuffer[recBufferBytePtr+i ] = 0x00;
recBuffer[recBufferBytePtr+i+1] = (byte)0x80;
} else {
int s = (int)( 0.5f + sample ); // Here, dithering would be more appropriate
recBuffer[recBufferBytePtr+i ] = (byte)(s & 0xFF);
recBuffer[recBufferBytePtr+i+1] = (byte)(s >> 8 & 0xFF);
}
i += 2;
}
// Do other stuff like saving the part of buffer to a file
// if ( reallySampledBytes > 0 ) { ... save recBuffer+recBufferBytePtr, length: reallySampledBytes
// Then move the recording pointer to the next position in the recording buffer
recBufferBytePtr += reallySampledBytes;
// Wrap around at the end of the recording buffer, e.g. like so:
if ( recBufferBytePtr >= recBufferByteSize ) {
recBufferBytePtr = 0;
sampleBytes = frameByteSize;
} else {
sampleBytes = recBufferByteSize - recBufferBytePtr;
if ( sampleBytes > frameByteSize )
sampleBytes = frameByteSize;
}
}
Thanks to Hartmut and beworker for the solution. Hartmut's code did worked at near 12-14 dB. I did merged the code from the sonic library too to increase volume, but that increase too much noise and distortion, so I kept the volume at 1.5-2.0 and instead tried to increase gain. I got decent sound volume which doesn't sound too loud in phone, but when listened on a PC sounds loud enough. Looks like that's the farthest I could go.
I am posting my final code to increase the loudness. Be aware that using increasing mVolume increases too much noise. Try to increase gain instead.
private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener() {
#Override
public void onPeriodicNotification(AudioRecord recorder) {
aRecorder.read(bBuffer, bBuffer.capacity()); // Fill buffer
if (getState() != State.RECORDING)
return;
try {
if (bSamples == 16) {
shBuffer.rewind();
int bLength = shBuffer.capacity(); // Faster than accessing buffer.capacity each time
for (int i = 0; i < bLength; i++) { // 16bit sample size
short curSample = (short) (shBuffer.get(i) * gain);
if (curSample > cAmplitude) { // Check amplitude
cAmplitude = curSample;
}
if(mVolume != 1.0f) {
// Adjust output volume.
int fixedPointVolume = (int)(mVolume*4096.0f);
int value = (curSample*fixedPointVolume) >> 12;
if(value > 32767) {
value = 32767;
} else if(value < -32767) {
value = -32767;
}
curSample = (short)value;
/*scaleSamples(outputBuffer, originalNumOutputSamples, numOutputSamples - originalNumOutputSamples,
mVolume, nChannels);*/
}
shBuffer.put(curSample);
}
} else { // 8bit sample size
int bLength = bBuffer.capacity(); // Faster than accessing buffer.capacity each time
bBuffer.rewind();
for (int i = 0; i < bLength; i++) {
byte curSample = (byte) (bBuffer.get(i) * gain);
if (curSample > cAmplitude) { // Check amplitude
cAmplitude = curSample;
}
bBuffer.put(curSample);
}
}
bBuffer.rewind();
fChannel.write(bBuffer); // Write buffer to file
payloadSize += bBuffer.capacity();
} catch (IOException e) {
e.printStackTrace();
Log.e(NoobAudioRecorder.class.getName(), "Error occured in updateListener, recording is aborted");
stop();
}
}
#Override
public void onMarkerReached(AudioRecord recorder) {
// NOT USED
}
};
simple use MPEG_4 format
To increase the call recording volume use AudioManager as follows:
int deviceCallVol;
AudioManager audioManager;
Start Recording:
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
//get the current volume set
deviceCallVol = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
//set volume to maximum
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0);
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(32);
recorder.setAudioSamplingRate(44100);
Stop Recording:
//revert volume to initial state
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, deviceCallVol, 0);
In my app I use an open source sonic library. Its main purpose is to speed up / slow down speech, but besides this it allows to increase loudness too. I apply it to playback, but it must work for recording similarly. Just pass your samples through it before compressing them. It has a Java interface too. Hope this helps.
What is the best way to play a sound and stop it?
I tried using RingtoneManager, MediaPlayer and SoundPool, but failed to stop the sound.
Is there a way to stop sound when using RingtoneManager TYPE_ALARM?
Please a simple snippet.
This is the last thing I tried:
SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
List<Integer> streams = new ArrayList<Integer>();
int soundId = pool.load(getApplicationContext(), R.drawable.alarm, 1); //There are several versions of this, pick which fits your sound
try{
if(myWifiInfo.getRssi() < -55)
{
/*
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();*/
Thread.sleep(1000);
int streamId = -1;
streamId = pool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamId);
textRssi.setText(String.valueOf(myWifiInfo.getRssi() + " WARNING"));
}
else {
Log.e("WIFI","Usao");
for (Integer stream : streams) {
pool.stop(stream);
}
streams.clear();
Log.e("WIFI","Izasao");
}
I'm assuming this function gets called many times? The problem is that you're adding the streamID to a local list. That list is getting recreated each time the function is called, so when you try to call stop the list will always be empty.
If its only being called once, then the problem is you're never calling stop, only play (they're in separate branches of that if).
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 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();
}
}
}