2013-07-05 110 views
0

嗨我已阅读一些post具有相同的问题,但无法找到确切的或我必须说我一直在寻找的答案。那么我只是想知道如何获得mediaplayer上设置的音频文件的播放级别。我已经尝试了int volume_level = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);,但从我看到的。我只能获取设备上的当前音量。那么我想达到的是添加一个动画,随着我的音频播放水平。这里是我到目前为止的代码:通过获取mediaPlayer的振幅来生成进度条

播放音频方法的调用之前:

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()之前,仍无法正常工作。

+0

您是否得到任何解决方案? – user1517153

+0

不是。但我想我可能会在下次再尝试这样做。 :d – KaHeL

回答

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