通过获取mediaPlayer的幅度来动画进度条

时间:2013-07-05 10:05:45

标签: android

您好我已经阅读了一些post同样的问题,但找不到确切的答案,或者我必须说出我一直在寻找的答案。好吧,我只想知道如何获得媒体播放器上设置的音频文件的播放级别。我已经尝试了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.prepare()仍无效之前添加audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

1 个答案:

答案 0 :(得分:1)

我所知道的唯一解决方案是使用Visualizer类。为方便起见,我建议使用KitKat live wallpaper sources中的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;
}