2011-12-06 49 views

回答

7

http://developer.android.com/reference/android/media/AudioRecord.html

當你讀緩衝字節值將代表幅度。字節值越高,聲音越大。

下面是一些我在一個應用程序中使用的縮小版,我寫了一段時間後:

添加到您的mainifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />

soundlevel.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ToggleButton 
     android:id="@+id/togglebutton_record" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ToggleButton" /> 


    <ProgressBar 
     android:id="@+id/progressbar_level" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

SoundLevel.java

import android.app.Activity; 
import android.media.AudioFormat; 
import android.media.AudioRecord; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.widget.CompoundButton; 
import android.widget.ProgressBar; 
import android.widget.ToggleButton; 

public class SoundLevel extends Activity { 

    private static final int sampleRate = 11025; 
    private static final int bufferSizeFactor = 10; 

    private AudioRecord audio; 
    private int bufferSize; 

    private ProgressBar level; 

    private Handler handler = new Handler(); 

    private int lastLevel = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.soundlevel); 

     level = (ProgressBar) findViewById(R.id.progressbar_level); 

     level.setMax(32676); 

     ToggleButton record = (ToggleButton) findViewById(R.id.togglebutton_record); 

     record.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 

       if (isChecked) { 
        bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT) * bufferSizeFactor; 

        audio = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); 

        audio.startRecording(); 

        Thread thread = new Thread(new Runnable() { 
         public void run() { 
          readAudioBuffer(); 
         }  
         }); 

        thread.setPriority(Thread.currentThread().getThreadGroup().getMaxPriority()); 

        thread.start(); 

        handler.removeCallbacks(update); 
        handler.postDelayed(update, 25); 

       } else if (audio != null) { 
        audio.stop(); 
        audio.release(); 
        audio = null; 
        handler.removeCallbacks(update); 
       } 

      } 
     }); 
    } 

    private void readAudioBuffer() { 

     try { 
      short[] buffer = new short[bufferSize]; 

      int bufferReadResult; 

      do { 

       bufferReadResult = audio.read(buffer, 0, bufferSize); 

       for (int i = 0; i < bufferReadResult; i++){ 

        if (buffer[i] > lastLevel) { 
         lastLevel = buffer[i]; 
        } 

       } 

      } while (bufferReadResult > 0 && audio.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING); 

      if (audio != null) { 
       audio.release(); 
       audio = null; 
       handler.removeCallbacks(update); 

      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    private Runnable update = new Runnable() { 

     public void run() { 

      SoundLevel.this.level.setProgress(lastLevel); 

      lastLevel *= .5; 

      handler.postAtTime(this, SystemClock.uptimeMillis() + 500); 

     } 

    }; 
} 
+0

感謝您的回覆。你可以給我一個代碼實現的例子。幫助我吧。 –

+1

編輯我的答案並添加示例代碼。 – jawsware

+0

非常非常有用! – Antonio