/**
* 数字音频合成
*
* @time 下午12:00:21
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Audio_synthesisActivity extends Activity implements OnClickListener {
private Button btn_start, btn_stop;
private boolean keepGoing = false;
private AudioSynthesisTask synthesisTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
@Override
public void onClick(View v) {
if (v == btn_start) {// 开启音频合成任务,修改状态
keepGoing = true;
synthesisTask = new AudioSynthesisTask();
synthesisTask.execute();
btn_stop.setEnabled(true);
} else if (v == btn_stop) {// 停止合成任务,修改状态
keepGoing = false;
btn_stop.setEnabled(false);
btn_start.setEnabled(true);
}
}
/**
* 实例化控件
*/
private void findView() {
btn_start = (Button) this.findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);
btn_stop = (Button) this.findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(this);
btn_stop.setEnabled(false);
}
@Override
protected void onPause() {
super.onPause();
keepGoing = false;
btn_start.setEnabled(true);
btn_stop.setEnabled(false);
}
/**
* 音频合成任务
*/
private class AudioSynthesisTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
final int SAMPLE_RATE = 11025;
int minSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, //
AudioFormat.CHANNEL_IN_MONO,// 常量: 16(0x00000010)
AudioFormat.ENCODING_PCM_16BIT);// PCM音频数据格式:16位,每样
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,// 媒体音量
SAMPLE_RATE,//
AudioFormat.CHANNEL_IN_MONO,// 常量: 16(0x00000010)
AudioFormat.ENCODING_PCM_16BIT, // PCM音频数据格式:每个采样点16比特
minSize,//
// MODE_STATIC 音频数据放到一个固定的buffer, 然后一次写入
//MODE_STREAM 通过write方式把数据一次一次得写到audiotrack中
AudioTrack.MODE_STREAM//
);
audioTrack.play();
short[] buffer = { //
8130, 15752, 22389, 27625, 31134, 32695, 32210, 29711, 25354, 19410, 12253, //
4329, -3865, -11818, -19032, -25055, -29511, -32121, -32722, -31276, -27874, -22728, -16160, -8582, -466 //
};
while (keepGoing) {
audioTrack.write(buffer, 0, buffer.length);
}
return null;
}
}
}
* 数字音频合成
*
* @time 下午12:00:21
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Audio_synthesisActivity extends Activity implements OnClickListener {
private Button btn_start, btn_stop;
private boolean keepGoing = false;
private AudioSynthesisTask synthesisTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
@Override
public void onClick(View v) {
if (v == btn_start) {// 开启音频合成任务,修改状态
keepGoing = true;
synthesisTask = new AudioSynthesisTask();
synthesisTask.execute();
btn_stop.setEnabled(true);
} else if (v == btn_stop) {// 停止合成任务,修改状态
keepGoing = false;
btn_stop.setEnabled(false);
btn_start.setEnabled(true);
}
}
/**
* 实例化控件
*/
private void findView() {
btn_start = (Button) this.findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);
btn_stop = (Button) this.findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(this);
btn_stop.setEnabled(false);
}
@Override
protected void onPause() {
super.onPause();
keepGoing = false;
btn_start.setEnabled(true);
btn_stop.setEnabled(false);
}
/**
* 音频合成任务
*/
private class AudioSynthesisTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
final int SAMPLE_RATE = 11025;
int minSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, //
AudioFormat.CHANNEL_IN_MONO,// 常量: 16(0x00000010)
AudioFormat.ENCODING_PCM_16BIT);// PCM音频数据格式:16位,每样
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,// 媒体音量
SAMPLE_RATE,//
AudioFormat.CHANNEL_IN_MONO,// 常量: 16(0x00000010)
AudioFormat.ENCODING_PCM_16BIT, // PCM音频数据格式:每个采样点16比特
minSize,//
// MODE_STATIC 音频数据放到一个固定的buffer, 然后一次写入
//MODE_STREAM 通过write方式把数据一次一次得写到audiotrack中
AudioTrack.MODE_STREAM//
);
audioTrack.play();
short[] buffer = { //
8130, 15752, 22389, 27625, 31134, 32695, 32210, 29711, 25354, 19410, 12253, //
4329, -3865, -11818, -19032, -25055, -29511, -32121, -32722, -31276, -27874, -22728, -16160, -8582, -466 //
};
while (keepGoing) {
audioTrack.write(buffer, 0, buffer.length);
}
return null;
}
}
}