I'm working on an app that will both record an audio file, and then have the option to play back that file once it's been recorded. The UI has an EQ component that animates relative to the current amplitude of the recording. I've got the animation working via the MediaRecorder.getMaxAmplitude()
method, but can't find any means to do this with MediaPlayer. I know it must be possible since there are music visualization Live Wallpapers by default that perform this functionality but I can't see any way that it's pulling that information when combing through AOSP. Does anybody know how to make this work?

- 37,901
- 21
- 84
- 115

- 869
- 1
- 9
- 17
-
4I'm confused by your accepted answer, it appears it didn't solve your problem from the comments, is that correct? – Matt Wolfe Aug 07 '14 at 06:48
-
Not only that, but another similarly wrong answer to this question got 31 upvotes below. – Dominic Cerisano Aug 18 '19 at 03:47
7 Answers
You can get the current volume of media player with the help of Audiomanager class.The code is as follows:-
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
Similarly,if you want to set the default volume of media player.You can do that like as:-
am.setStreamVolume(
AudioManager.STREAM_MUSIC,
volume_level,
0);
That's all..Happy coding :)

- 4,999
- 5
- 51
- 61
-
8That is not what he is asking. He is asking for the current output level, not the volume setting. – Dominic Cerisano Dec 24 '14 at 01:13
-
Please read my answer carefully. I have explained both the things . – Deepak Sharma Oct 29 '18 at 07:20
-
2He is asking for the running realtime waveform amplitude data (current output level), not the volume setting of the player, which is all you suggested. – Dominic Cerisano Oct 30 '18 at 00:13
-
You are in luck. There is a class called Visualizer which will do what you want I think.
import android.app.Activity;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private Visualizer audioOutput = null;
public float intensity = 0; //intensity is a value between 0 and 1. The intensity in this case is the system output volume
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createVisualizer();
}
private void createVisualizer(){
int rate = Visualizer.getMaxCaptureRate();
audioOutput = new Visualizer(0); // get output audio stream
audioOutput.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {
intensity = ((float) waveform[0] + 128f) / 256;
Log.d("vis", String.valueOf(intensity));
}
@Override
public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {
}
},rate , true, false); // waveform not freq data
Log.d("rate", String.valueOf(Visualizer.getMaxCaptureRate()));
audioOutput.setEnabled(true);
}
}
you will need these permissions:
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>

- 977
- 1
- 12
- 22
-
1Visualizer is possibly the least cross platform class in Java. Guaranteed to require subclassing on at least one target platform (linux, mac, win, arm, etc in combo with certain sound chips). Worse than the Camera class, even. Is also a CPU hog. Better to implement getLevel() on a data line for the OPs requirements. It is just a buffer so works everywhere. – Dominic Cerisano Nov 05 '18 at 02:47
-
1
I think you have to use AudioManager. As the API states it can be used for volume control:
AudioManager provides access to volume and ringer mode control.
Use Context.getSystemService(Context.AUDIO_SERVICE) to get an instance of this class.
Then I think this method would be useful.
-
12That's where I was poking around but that method returns the currently set volume level of the device, not the playback level. – Scott Ferguson May 28 '10 at 13:41
-
Where is the difference? Maybe you are asking for the volume of the wrong stream? – Janusz Jul 05 '10 at 11:05
-
5It's not a question of the volume for the current playback stream, but the volume of the actual media being played back. Just because your music stream volume is set at 60% doesn't mean the volume of the sound at that moment is at that same level. It could be a quiet part of the song. – MattC Aug 28 '12 at 22:13
-
2He is asking for access to the audio stream sample data, not the volume setting of the player, which is just a scaling multiplier. Why is this the accepted answer? You could at least accept the Visualizer solution, even though that class is more trouble than it is worth. – Dominic Cerisano Dec 15 '18 at 03:22
As follow:
audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
int volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolumeLevel = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int volumePercent = (int) (((float) volumeLevel / maxVolumeLevel) * 100);

- 1,386
- 17
- 16
You have to implement getLevel() in DataLine. This is as close to the bus as it gets in Java.
This involves calculating a running average of amplitude (waveform) data from the sound stream buffer.
This causes a lot of bus traffic to access the buffer, so left as abstract method.
Root Mean Square (RMS) is one approach:
https://community.oracle.com/message/5391003
DSP with FFT (eg. Visualizer class) gives a complete frequency spectrum, but consumes much more CPU resources.
Don't slam on full DSP if all you need is RMS (eg. simple beat detection). Limit the quality of your samples to improve performance.

- 3,522
- 1
- 31
- 44
I've been looking for a way to do something similar for a while. I really want to be able to see the volume/amplitude of anything being played over the media stream, but I'll settle for being able to do it for something I'm currently playing.
So far, the best solution I've found is implemented in RingDroid. I haven't looked into the code too deeply, but it looks like the way that RingDroid creates its soundmap is by actually analyzing the sound file bit by bit.
I've considered using a similar approach and then displaying a visualizer that runs separate from the audio file, but runs at a synchronized pace. However, this seems like too much work for something that should be way simpler.

- 1
- 1

- 850
- 2
- 9
- 21
I got this solution:
final int volume_level = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = (float) volume_level / maxVolume;

- 9,564
- 146
- 81
- 122

- 395
- 4
- 17
-
This just tells you how high you can set the volume. The OP is looking for realtime amplitude measurements. – Dominic Cerisano Aug 29 '19 at 14:58