开发者

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

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 combi开发者_运维技巧ng through AOSP. Does anybody know how to make this work?


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


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>


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.


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


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜