android – How do I get the current volume/amplitude in a MediaPlayer?-ThrowExceptions

Exception or error:

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?

How to solve:

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.

to get an instance of this class.

Then I think this method would be useful.


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:-


That’s all..Happy coding 🙂


You are in luck. There is a class called Visualizer which will do what you want I think.

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
protected void onCreate(Bundle savedInstanceState) {


private void createVisualizer(){
    int rate = Visualizer.getMaxCaptureRate();
    audioOutput = new Visualizer(0); // get output audio stream
    audioOutput.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
        public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {
            intensity = ((float) waveform[0] + 128f) / 256;
            Log.d("vis", String.valueOf(intensity));

        public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {

    },rate , true, false); // waveform not freq data
    Log.d("rate", String.valueOf(Visualizer.getMaxCaptureRate()));


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>


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:

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.


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.


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);


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;

Leave a Reply

Your email address will not be published. Required fields are marked *