Android에서 MIC를 사용하여 오디오를 mp3 형식으로 녹음하고 싶습니다. 아래 코드를 녹음하려고하는데 sdcard (경로 : sdcard / recording.mp3)를 확인하면 파일이 작동하지 않습니다. (파일 크기는 0kb입니다).
mp3 또는 wav 형식으로 Android에서 마이크 오디오를 녹음하려면 어떻게해야합니까? 도와주세요.
private final int FREQUENCY = 11025;
private final int CUSTOM_FREQ_SOAP = 1;
private final int OUT_FREQUENCY = FREQUENCY * CUSTOM_FREQ_SOAP;
private final int CHANNEL_CONTIGURATION = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private final int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
======
try {
mRecordingFile = File.createTempFile("recording", ".mp3", new File("/sdcard/"));
} catch (IOException e) {
throw new RuntimeException("Couldn't create file on SD card", e);
}
======
private class RecordAudio extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
isRecording = true;
try {
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(
mRecordingFile, true)));
int bufferSize = AudioRecord.getMinBufferSize(FREQUENCY,
CHANNEL_CONTIGURATION, AUDIO_ENCODING);
AudioRecord audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC, FREQUENCY,
CHANNEL_CONTIGURATION, AUDIO_ENCODING, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0,
bufferSize);
int amplitude = 0;
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
amplitude += Math.abs((int) buffer[i]);
}
final int amp = amplitude;
}
audioRecord.stop();
//dos.close();
} catch (Throwable t) {
}
return null;
}
public void recordAudio(String fileName) {
final MediaRecorder recorder = new MediaRecorder();
ContentValues values = new ContentValues(3);
values.put(MediaStore.MediaColumns.TITLE, fileName);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile("/sdcard/sound/" + fileName);
try {
recorder.prepare();
} catch (Exception e){
e.printStackTrace();
}
final ProgressDialog mProgressDialog = new ProgressDialog(MyActivity.this);
mProgressDialog.setTitle(R.string.lbl_recording);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mProgressDialog.dismiss();
recorder.stop();
recorder.release();
}
});
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface p1) {
recorder.stop();
recorder.release();
}
});
recorder.start();
mProgressDialog.show();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다