/**
* 原始音频的播放和录制
*
* audio
*
* audioTrack
*
*
* @time 下午12:58:03
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@SuppressWarnings("static-access")
public class AltAudioRecorderActivity extends Activity implements OnCompletionListener, OnClickListener {
private Button btn_stop_play, btn_start_play, btn_start_record, btn_stop_record;
private File audioFile;
private int frequency = 11025;
private int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
private RecordAudio recordAudio;
private PlayRecord playRecord;
private boolean isRecording = false;
private boolean isPlaying = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alt_recoder);
findView();
// 创建目录
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Andoroid/data/cn.yue.mrdemo.CustomRecorderActivity/files/");
file.mkdirs();
try {
audioFile = file.createTempFile("recording", ".pcm", file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 实例化控件
*/
private void findView() {
btn_start_record = (Button) this.findViewById(R.id.btn_alt_start_recording);
btn_stop_record = (Button) this.findViewById(R.id.btn_alt_stop_recording);
btn_start_play = (Button) this.findViewById(R.id.btn_alt_start_palyback);
btn_stop_play = (Button) this.findViewById(R.id.btn_alt_stop_playback);
btn_start_record.setOnClickListener(this);
btn_stop_record.setOnClickListener(this);
btn_start_play.setOnClickListener(this);
btn_stop_play.setOnClickListener(this);
btn_stop_record.setEnabled(false);
btn_start_play.setEnabled(false);
btn_stop_play.setEnabled(false);
}
@Override
public void onClick(View v) {
if (v == btn_start_record) {
record();
} else if (v == btn_stop_record) {
stopRecord();
} else if (v == btn_start_play) {
play();
} else if (v == btn_stop_play) {
stopPlay();
}
}
@Override
public void onCompletion(MediaPlayer mp) {
}
/********************************************/
/**
* 录制音频
*/
private void record() {
btn_start_record.setEnabled(false);
btn_stop_record.setEnabled(true);
btn_start_play.setEnabled(true);
recordAudio = new RecordAudio();
recordAudio.execute();
}
/**
* 停止录制音频
*/
private void stopRecord() {
isRecording = false;
}
/**
* 播放
*/
private void play() {
btn_start_play.setEnabled(true);
playRecord = new PlayRecord();
playRecord.execute();
btn_stop_play.setEnabled(true);
}
/**
* 停止播放
*/
private void stopPlay() {
isPlaying = false;
btn_start_play.setEnabled(true);
btn_stop_play.setEnabled(false);
}
/*************************************************/
/**
* 录音
*/
public class RecordAudio extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
isRecording = true;
try {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(audioFile)));
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
int r = 0;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
}
publishProgress(new Integer(r));
r++;
}
audioRecord.stop();
dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
/**
* 播放
*/
private class PlayRecord extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
isPlaying = true;
int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
short[] audiodata = new short[bufferSize];
try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(audioFile)));
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,//
frequency,//
channelConfiguration,//
audioEncoding,//
bufferSize, //
AudioTrack.MODE_STREAM//
);
audioTrack.play();
while (isPlaying && dis.available() > 0) {
int i = 0;
while (dis.available() > 0 && i < audiodata.length) {
audiodata[i] = dis.readShort();
i++;
}
audioTrack.write(audiodata, 0, audiodata.length);
}
dis.close();
btn_start_play.setEnabled(false);
btn_stop_play.setEnabled(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
* 原始音频的播放和录制
*
* audio
*
* audioTrack
*
*
* @time 下午12:58:03
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@SuppressWarnings("static-access")
public class AltAudioRecorderActivity extends Activity implements OnCompletionListener, OnClickListener {
private Button btn_stop_play, btn_start_play, btn_start_record, btn_stop_record;
private File audioFile;
private int frequency = 11025;
private int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
private RecordAudio recordAudio;
private PlayRecord playRecord;
private boolean isRecording = false;
private boolean isPlaying = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alt_recoder);
findView();
// 创建目录
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Andoroid/data/cn.yue.mrdemo.CustomRecorderActivity/files/");
file.mkdirs();
try {
audioFile = file.createTempFile("recording", ".pcm", file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 实例化控件
*/
private void findView() {
btn_start_record = (Button) this.findViewById(R.id.btn_alt_start_recording);
btn_stop_record = (Button) this.findViewById(R.id.btn_alt_stop_recording);
btn_start_play = (Button) this.findViewById(R.id.btn_alt_start_palyback);
btn_stop_play = (Button) this.findViewById(R.id.btn_alt_stop_playback);
btn_start_record.setOnClickListener(this);
btn_stop_record.setOnClickListener(this);
btn_start_play.setOnClickListener(this);
btn_stop_play.setOnClickListener(this);
btn_stop_record.setEnabled(false);
btn_start_play.setEnabled(false);
btn_stop_play.setEnabled(false);
}
@Override
public void onClick(View v) {
if (v == btn_start_record) {
record();
} else if (v == btn_stop_record) {
stopRecord();
} else if (v == btn_start_play) {
play();
} else if (v == btn_stop_play) {
stopPlay();
}
}
@Override
public void onCompletion(MediaPlayer mp) {
}
/********************************************/
/**
* 录制音频
*/
private void record() {
btn_start_record.setEnabled(false);
btn_stop_record.setEnabled(true);
btn_start_play.setEnabled(true);
recordAudio = new RecordAudio();
recordAudio.execute();
}
/**
* 停止录制音频
*/
private void stopRecord() {
isRecording = false;
}
/**
* 播放
*/
private void play() {
btn_start_play.setEnabled(true);
playRecord = new PlayRecord();
playRecord.execute();
btn_stop_play.setEnabled(true);
}
/**
* 停止播放
*/
private void stopPlay() {
isPlaying = false;
btn_start_play.setEnabled(true);
btn_stop_play.setEnabled(false);
}
/*************************************************/
/**
* 录音
*/
public class RecordAudio extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
isRecording = true;
try {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(audioFile)));
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
int r = 0;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
}
publishProgress(new Integer(r));
r++;
}
audioRecord.stop();
dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
/**
* 播放
*/
private class PlayRecord extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
isPlaying = true;
int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
short[] audiodata = new short[bufferSize];
try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(audioFile)));
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,//
frequency,//
channelConfiguration,//
audioEncoding,//
bufferSize, //
AudioTrack.MODE_STREAM//
);
audioTrack.play();
while (isPlaying && dis.available() > 0) {
int i = 0;
while (dis.available() > 0 && i < audiodata.length) {
audiodata[i] = dis.readShort();
i++;
}
audioTrack.write(audiodata, 0, audiodata.length);
}
dis.close();
btn_start_play.setEnabled(false);
btn_stop_play.setEnabled(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}