您好我读过一些post有同样的问题,但无法找到确切的或我必须说,我一直在寻找的答案。好吧,我只是想知道我怎样才能得到它被设置在媒体播放器的音频文件的播放水平。我已经尝试过了 INT volume_level = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
但是从我所看到的。我只得到当前的音量我的设备上设置。那么我想才达到的增加与我的音量播放下面的动画。这是我的code到目前为止:
Hi I've read some post with same question but can't find the exact or I must say the answer I've been looking for. Well I just want to know how I can get the playback level of the audio file that is set on the mediaplayer. I already tried the int volume_level = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
but from what I see. I only get the current volume set on my device. Well what I want to achive is to add an animation that follows with level of my audio being played. Here's my code so far:
在发挥音响的方法调用:
before the call of play audio method:
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
播放方式:
public void playAudio(String record_path) throws IOException{
if(audioPlayer!=null && mpStatus == State.Paused){
/*play from paused state*/
audioPlayer.start();
mpStatus = State.Playing;
}
else
{
/*play from start of recording*/
setMediaPlayer(record_path);
audioPlayer.start();
mpStatus = State.Playing;
}
}
和螺纹:
private class playBackRunnable extends Thread {
final long start_time = System.currentTimeMillis();
public void run() {
while(chk_play.isChecked()){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
final long elapsed = System.currentTimeMillis() - start_time;
final String elapsed_time = util.getAsTime((int) elapsed);
runOnUiThread(new Runnable() {
@Override
public void run() {
int volume_level = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int amp = (int)(volume_level * 100.f)/100;
Log.v("Volume Level", String.valueOf(amp));
if(chk_play.isChecked()){
prog_volume.setProgress(amp);
//txt_rectime.setText(elapsed_time);
if(amp <= 40 ){
prog_volume.setProgressDrawable(getResources().getDrawable(R.drawable.progress_green));
}else if(amp <= 60){
prog_volume.setProgressDrawable(getResources().getDrawable(R.drawable.progress_yellow));
}else if(amp <= 80){
prog_volume.setProgressDrawable(getResources().getDrawable(R.drawable.progress_orange));
}else {
prog_volume.setProgressDrawable(getResources().getDrawable(R.drawable.progress_red));
}
}
}
});
}
}
}
希望有人能帮助我。先谢谢了。
Hope someone can help me with this. Thanks in advance.
编辑:
新增 audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
前audioPlayer prepare()仍无法正常工作
Added audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
before audioPlayer.prepare() still not working.
我知道的唯一解决方案使用可视化类。为了方便的原因,我建议使用AudioCapture.java从奇巧动态壁纸来源的,里面加了可视化的数据处理层。上面还挂的项目为用途的例子,这里是我对自己使用它JUnits测试:
The only solution I know uses the Visualizer class. For convenience reason, I suggest using AudioCapture.java from KitKat live wallpaper sources, which add a data processing layer over Visualizer. The project linked above also gives some examples of uses, and here is how I use it in myself for JUnits tests :
private int getAudioOutputAmplitude(int durationInSeconds) throws InterruptedException {
AudioCapture mAudioCapture = new AudioCapture(AudioCapture.TYPE_PCM, 1024);
mAudioCapture.start();
Thread.sleep(durationInSeconds * 1000);
int [] mVizData;
mVizData = mAudioCapture.getFormattedData(1, 1);
mAudioCapture.release();
int minValue = 0;
int maxValue = 0;
for (int value:mVizData){
if (value<minValue){
minValue = value;
} else if (value>maxValue){
maxValue = value;
}
}
return maxValue-minValue;
}