How do I get the current volume/amplitude in a MediaPlayer?

前端 未结 7 916
孤街浪徒
孤街浪徒 2020-12-13 20:26

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 animat

7条回答
  •  没有蜡笔的小新
    2020-12-13 20:40

    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:

    
    
    

提交回复
热议问题