2

嗨,我读过一些有同样问题的帖子,但找不到确切的答案,或者我必须说出我一直在寻找的答案。好吧,我只想知道如何获得在媒体播放器上设置的音频文件的播放级别。我已经尝试过, 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() 仍然无法正常工作之前添加。

4

1 回答 1

4

我知道的唯一解决方案是使用 Visualizer 类。为方便起见,我建议使用来自KitKat 动态壁纸源的AudioCapture.java ,它在 Visualizer 上添加了一个数据处理层。上面链接的项目还提供了一些使用示例,以下是我自己在 JUnits 测试中使用它的方式:

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;
}
于 2014-03-12T15:37:39.783 回答