嗨,我读过一些有同样问题的帖子,但找不到确切的答案,或者我必须说出我一直在寻找的答案。好吧,我只想知道如何获得在媒体播放器上设置的音频文件的播放级别。我已经尝试过, int volume_level = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
但是从我所看到的。我只获得设备上设置的当前音量。那么我想要实现的是添加一个动画,跟随我正在播放的音频级别。到目前为止,这是我的代码:
在调用播放音频方法之前:
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));
}
}
}
});
}
}
}
希望有人可以帮助我。提前致谢。
编辑:
audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
在 audioPlayer.prepare() 仍然无法正常工作之前添加。