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

前端 未结 7 873
孤街浪徒
孤街浪徒 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:35

    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);
    
    0 讨论(0)
  • 2020-12-13 20:39

    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;
    
    0 讨论(0)
  • 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:

    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
    
    0 讨论(0)
  • 2020-12-13 20:45

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

    0 讨论(0)
  • 2020-12-13 20:49

    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.

    0 讨论(0)
  • 2020-12-13 20:53

    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.

    0 讨论(0)
提交回复
热议问题