android 使用 audiorecord 和 audiotrack 实现实时录音播放

基本思路就是用 audiorecord不断得到音频数据,然后使用audiotrack 播放


//得到音频

package com.ysg.audiotest;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

/*
 * To getaudio or play audio
 * */
public class mAudio {
	private AudioRecord audioRecord;
	private Context context;
	private boolean isRecording = false ;
	private PipedOutputStream outstream ;//利用管道传输数据
	public mAudio(Context context , PipedInputStream instream) throws IOException {
		this.context  = context;
		//初始化管道流 用于向外传输数据
		outstream = new PipedOutputStream();
		outstream.connect(instream);
	}
	public void StartAudioData(){//得到录音数据
		int frequency = 11025;
		@SuppressWarnings("deprecation")
		int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
		int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
		int buffersize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
		audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
			frequency, channelConfiguration, audioEncoding, buffersize);
		byte[]buffer  = new byte[buffersize];
		audioRecord.startRecording();//开始录音
		isRecording = true;
		 int bufferReadSize = 1024; 
		while (isRecording){
			audioRecord.read(buffer, 0, bufferReadSize);
			try {
				outstream.write(buffer, 0, bufferReadSize);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public void stopRecord(){//停止录音
		isRecording = false;
		audioRecord.stop();
		try {
			outstream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


播放音频

package com.ysg.audiotest;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

public class mAudioPlayer {
	private PipedInputStream instream;
	private boolean isPlaying ;
	private AudioTrack audioplayer;
	private byte[] buffer;
	public mAudioPlayer() {
		isPlaying = false;
		instream = null;
		//初始化播音类
		@SuppressWarnings("deprecation")
		int bufsize = AudioTrack.getMinBufferSize(11025, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
				AudioFormat.ENCODING_PCM_16BIT);
		audioplayer = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_STEREO, 
				AudioFormat.ENCODING_PCM_16BIT, bufsize,AudioTrack.MODE_STREAM);
	}
	//设置管道流,用于接受音频数据
	public void setOutputStream(PipedOutputStream out) throws IOException{
		instream = new PipedInputStream(out);
		
	}
	public void startPlayAudio(){ //调用之前先调用setOutputStream 函数
		isPlaying = true;
		audioplayer.play();//开始接受数据流播放
		buffer = new byte[1024];
		while (instream!=null&&isPlaying){
			try {
				while (instream.available()>0){
					int size = instream.read(buffer);
					audioplayer.write(buffer, 0
							, size);//不断播放数据
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public void stopPlay(){//停止播放
		isPlaying = false ;
		try {
			instream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		audioplayer.stop();
	}
	
}

activity

package 
com.ysg.audiotest;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
	PipedInputStream in;
	boolean isRrcord;
	mAudio mm ;
	mAudioPlayer m;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 
		 isRrcord = false;
		 
	}
	public void btnclick(View v){
		if (isRrcord){
			isRrcord = false;
			mm.stopRecord();
			m.stopPlay();
		}else{
			isRrcord = true;
			startRecord();
		}
	}
	private void startRecord(){
		in = new PipedInputStream();
      new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					mm = new mAudio(MainActivity.this, in);
					mm.StartAudioData();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
		 new Thread(new Runnable() {
			@Override
			public void run() {
				byte[] buffer = new byte[1024];
				PipedOutputStream pout = new PipedOutputStream();
			    m = new mAudioPlayer();
				try {
					m.setOutputStream(pout);
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							m.startPlayAudio();
						}
					}).start();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				int size = 0 ;
				try {
					while (true){
						while (in.available()>0){
							size = in.read(buffer);
							pout.write(buffer, 0, size);
						}
					}
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
}


再者注意添加权限:

   android:name="android.permission.RECORD_AUDIO"
   />



你可能感兴趣的:(android 使用 audiorecord 和 audiotrack 实现实时录音播放)