¿ªÆô×ó²à

android ÉèÖÃÏ̵߳ÄÓÅÏȼ¶

¼¼Êõ
Ö»ÄÜÔÚÕæ»úÉÏʵÏÖ£¬Ä£Äâ»ú²»Ö§³Ö
±¾´úÂëÊǰ³½è¼øµÄ£¬²¢¼ÓÈëÁËÏêϸµÄ×¢½â£¬Ö÷ÒªÓõ½AudioRecord£¬ºÍAudioTrackÁ½¸öÀà
¼òµ¥ËµÒ»ÏÂÁ½¸öÀàµÄ¹¹Ô캯ÊýºÍ»ù±¾·½·¨£º
AudioRecord
public AudioRecord (int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes) Ëĸö²ÎÊý·Ö±ðÊÇ
audioSource

ÒôƵԴ£ºÖ¸µÄÊÇ´ÓÄÄÀï²É¼¯ÒôƵ¡£ÕâÀïÎÒÃǵ±È»ÊÇ´ÓÂó¿Ë·ç²É¼¯ÒôƵ£¬ËùÒԴ˲ÎÊýµÄֵΪMIC
sampleRateInHz

²ÉÑùÂÊ£ºÒôƵµÄ²ÉÑùƵÂÊ£¬Ã¿ÃëÖÓÄܹ»²ÉÑùµÄ´ÎÊý£¬²ÉÑùÂÊÔ½¸ß£¬ÒôÖÊÔ½¸ß¡£¸ø³öµÄʵÀýÊÇ44100¡¢22050¡¢11025µ«²»ÏÞÓÚÕ⼸¸ö²ÎÊý¡£ÀýÈçÒª²É¼¯µÍÖÊÁ¿µÄÒôƵ¾Í¿ÉÒÔʹÓÃ4000¡¢8000µÈµÍ²ÉÑùÂÊ¡£
channelConfig
ÉùµÀÉèÖãºandroidÖ§³ÖË«ÉùµÀÁ¢ÌåÉùºÍµ¥ÉùµÀ¡£MONOµ¥ÉùµÀ£¬STEREOÁ¢ÌåÉù
audioFormat


±àÂëÖÆÊ½ºÍ²ÉÑù´óС£º²É¼¯À´µÄÊý¾Ýµ±È»Ê¹ÓÃPCM±àÂë(Âö³å´úÂëµ÷ÖÆ±àÂ룬¼´PCM±àÂë¡£PCMͨ¹ý³éÑù¡¢Á¿»¯¡¢±àÂëÈý¸ö²½Ö轫Á¬Ðø±ä»¯µÄÄ£ÄâÐźÅת»»ÎªÊý×Ö±àÂë¡£) androidÖ§³ÖµÄ²ÉÑù´óС16bit »òÕß8bit¡£µ±È»²ÉÑù´óСԽ´ó£¬ÄÇôÐÅÏ¢Á¿Ô½¶à£¬ÒôÖÊÒ²Ô½¸ß£¬ÏÖÔÚÖ÷Á÷µÄ²ÉÑù´óС¶¼ÊÇ16bit£¬ÔÚµÍÖÊÁ¿µÄÓïÒô´«ÊäµÄʱºò8bit ×ã¹»ÁË¡£
bufferSizeInBytes
²É¼¯Êý¾ÝÐèÒªµÄ»º³åÇøµÄ´óС£¬Èç¹û²»ÖªµÀ×îСÐèÒªµÄ´óС¿ÉÒÔÔÚgetMinBufferSize()²é¿´¡£
getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioTrackÀà
AudioTrack±ÈAudioRecord¶à³öÀ´Ò»¸ö²ÎÊý
public AudioTrack (int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes£¬int mode)
ÆäÖÐstreamType
<!--[if !supportLists]-->l         <!--[endif]-->STREAM_ALARM£º¾¯¸æÉù
<!--[if !supportLists]-->l         <!--[endif]-->STREAM_MUSCI£ºÒôÀÖÉù£¬ÀýÈçmusicµÈ
<!--[if !supportLists]-->l         <!--[endif]-->STREAM_RING£ºÁåÉù
<!--[if !supportLists]-->l         <!--[endif]-->STREAM_SYSTEM£ºÏµÍ³ÉùÒô
<!--[if !supportLists]-->l         <!--[endif]-->STREAM_VOCIE_CALL£ºµç»°ÉùÒô
ÆäÖÐmode
AudioTrackÖÐÓÐMODE_STATICºÍMODE_STREAMÁ½ÖÖ·ÖÀà¡£STREAMµÄÒâ˼ÊÇÓÉÓû§ÔÚÓ¦ÓóÌÐòͨ¹ýwrite·½Ê½°ÑÊý¾ÝÒ»´ÎÒ»´ÎµÃдµ½audiotrackÖС£Õâ¸öºÍÎÒÃÇÔÚsocketÖз¢ËÍÊý¾ÝÒ»Ñù£¬Ó¦Óòã´Óij¸öµØ·½»ñÈ¡Êý¾Ý£¬ÀýÈçͨ¹ý±à½âÂëµÃµ½PCMÊý¾Ý£¬È»ºówriteµ½audiotrack¡£
ÕâÖÖ·½Ê½µÄ»µ´¦¾ÍÊÇ×ÜÊÇÔÚJAVA²ãºÍNative²ã½»»¥£¬Ð§ÂÊËðʧ½Ï´ó¡£
¶øSTATICµÄÒâ˼ÊÇÒ»¿ªÊ¼´´½¨µÄʱºò£¬¾Í°ÑÒôƵÊý¾Ý·Åµ½Ò»¸ö¹Ì¶¨µÄbuffer£¬È»ºóÖ±½Ó´«¸øaudiotrack£¬ºóÐø¾Í²»ÓÃÒ»´Î´ÎµÃwriteÁË¡£AudioTrack»á×Ô¼º²¥·ÅÕâ¸öbufferÖеÄÊý¾Ý¡£
ÕâÖÖ·½·¨¶ÔÓÚÁåÉùµÈÄÚ´æÕ¼ÓýÏС£¬ÑÓʱҪÇó½Ï¸ßµÄÉùÒôÀ´ËµºÜÊÊÓá£
´úÂ벿·Ö£º
1.²¼¾ÖÎļþ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:text="¿ªÊ¼±ß¼±ß·Å" android:id="@+id/recordButton" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="Í£Ö¹" android:id="@+id/stopButton" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<Button android:text="Í˳ö" android:id="@+id/exitButton" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
<TextView android:text="ÒôÁ¿µ÷½Ú" android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
<SeekBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/seekBar1"></SeekBar>
</LinearLayout>
2.ΪAudioRecordÉêÇëÐí¿É£¬ÐÞ¸ÃAndroidManifes.xmlÈçÏ£º
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.testrecord"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".textRecord"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         
        </activity>
     <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    </application>
</manifest>
3.Ö÷ҪʵÏÖ´úÂ룺
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
public class textRecord extends Activity {
   Button recordButton,stopButton,exitButton;
   SeekBar skbVolume;
   boolean isRecording = false;
   static final int frequency = 44100;
   static final int channelConfiguration=AudioFormat.CHANNEL_CONFIGURATION_MONO;
   static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
   int recBufSize,playBufSize;
   AudioRecord audioRecord;
   AudioTrack audioTrack;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle("ÖúÌýÆ÷");
        //ÓÃgetMinBufferSize()·½·¨µÃµ½²É¼¯Êý¾ÝËùÐèÒªµÄ×îС»º³åÇøµÄ´óС
        recBufSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
        playBufSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
        //ʵÀý»¯AudioRecord(ÉùÒôÀ´Ô´£¬²ÉÑùÂÊ£¬ÉùµÀÉèÖ㬲ÉÑùÉùÒô±àÂ룬»º´æ´óС£©
        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,frequency,channelConfiguration,audioEncoding,recBufSize);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,frequency,channelConfiguration,audioEncoding,playBufSize,AudioTrack.MODE_STREAM);
        recordButton =(Button)this.findViewById(R.id.recordButton);
        stopButton =(Button) this.findViewById(R.id.stopButton);
        exitButton =(Button)this.findViewById(R.id.exitButton);
        recordButton.setOnClickListener(new ClickEvent());
        stopButton.setOnClickListener(new ClickEvent());
        exitButton.setOnClickListener(new ClickEvent());
        skbVolume = (SeekBar)this.findViewById(R.id.seekBar1);
        skbVolume.setMax(100);
        skbVolume.setProgress(70);
    // ÉèÖÃÉùÒô´óС
        audioTrack.setStereoVolume(0.7f, 0.7f);
        skbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
   
   
   public void onStopTrackingTouch(SeekBar seekBar) {
    float vol = (float)(seekBar.getProgress())/(float)(seekBar.getMax());
    audioTrack.setStereoVolume(vol, vol);
   
   }
   
   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
   
   }
   
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    // TODO Auto-generated method stub
   
   }
  });
    }
    protected void onDestroy(){
     super.onDestroy();
// ɱËÀµ±Ç°½ø³Ì
     android.os.Process.killProcess(android.os.Process.myPid());
    }
    class ClickEvent implements View.OnClickListener{
     public void onClick(View v){
      if(v == recordButton){
       isRecording = true;
       new RecordPlayThread().start();
      }else if(v == stopButton){
       isRecording = false;
      }else if(v== exitButton){
       isRecording = false;
       textRecord.this.finish();
      }
     }
    }
    class RecordPlayThread extends Thread{
     public void run(){
      try{
//byte ÎļþÀ´´æ´¢ÉùÒô
       byte[] buffer = new byte[recBufSize];
//¿ªÊ¼²É¼¯ÉùÒô
       audioRecord.startRecording();
//²¥·ÅÉùÒô
       audioTrack.play();
       while(isRecording){
//´ÓMIC´æ´¢µ½»º´æÇø
        int bufferReadResult = audioRecord.read(buffer,0, recBufSize);
        byte[] tmpBuf = new byte[bufferReadResult];
        System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
//²¥·Å»º´æÇøµÄÊý¾Ý
        audioTrack.write(tmpBuf, 0, tmpBuf.length);

       }
       audioTrack.stop();
       audioRecord.stop();
      }catch(Throwable t){
       Toast.makeText(textRecord.this, t.getMessage(), 1000);
      }
     }
    };
}
ÊÕ²Ø
ËÍÔÞ
·ÖÏí

·¢±í»Ø¸´

µÇ¼ºó²ÅÄÜÆÀÂÛ

ÒôƵ·ÖÀà´óÈ«

+ ²é¿´È«²¿·ÖÀà