我想保存要存储在文件夹中的pcm文件格式文件和要保存在SD卡上的文件夹。到目前为止,我只能在SD卡上保存一个pcm音频文件。我试图在SD卡上存储多个pcm文件但是发生了错误。错误是我没有录制文件。每次我录制一些声音时,文件长度都保持为零kb。
private void startRecord() {
File path = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MYfolder/");
// File file = new File(path,"myrecording.pcm");
path.mkdirs();
// file = new File(path, "myrecording.pcm");
// // File file = new File(Environment.getExternalStorageDirectory(),
// // "test.pcm");
//
int sampleFreq = 11025; /* (Integer)spFrequency.getSelectedItem(); */
try {
audiofile = File.createTempFile("myrecording", ".pcm", path);
// recorder.setOutputFile(audiofile.getAbsolutePath());
// file.createNewFile();
// File.createTempFile("myrecording", ".pcm", file);
// OutputStream outputStream = new FileOutputStream(file);
OutputStream outputStream = new FileOutputStream(
audiofile.getAbsoluteFile());
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(
bufferedOutputStream);
int minBufferSize = AudioRecord
.getMinBufferSize(sampleFreq, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
short[] audioData = new short[minBufferSize];
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleFreq, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
audioRecord.startRecording();
while (recording) {
int numberOfShort = audioRecord.read(audioData, 0,
minBufferSize);
for (int i = 0; i < numberOfShort; i++) {
dataOutputStream.writeShort(audioData[i]);
}
}
audioRecord.stop();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
addRecordingToMediaLibrary();
}
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/pcm");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendStickyBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}
我编辑了我的代码,我想通过内容解析器在特定文件夹中保存sdcard上的pcm文件。我得到了内存泄漏错误,不知道如何解决这个问题。
答案 0 :(得分:0)
YOUR_FILE_NAME每次都应该不同,使用datetime设置文件名不同
private void startRecordinga() {
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
//convert short to byte
private byte[] short2byte(short[] sData) {
int shortArrsize = sData.length;
byte[] bytes = new byte[shortArrsize * 2];
for (int i = 0; i < shortArrsize; i++) {
bytes[i * 2] = (byte) (sData[i] & 0x00FF);
bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
sData[i] = 0;
}
return bytes;
}
private void writeAudioDataToFile() {
// Write the output audio in byte
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath()+"/Myfolder/"+YOUR_FILE_NAME+".pcm";
short sData[] = new short[BufferElements2Rec];
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format
recorder.read(sData, 0, BufferElements2Rec);
System.out.println("Short wirting to file" + sData.toString());
try {
// // writes the data to file from buffer
// // stores the voice buffer
byte bData[] = short2byte(sData);
os.write(bData, 0, BufferElements2Rec * BytesPerElement);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
// stops the recording activity
if (null != recorder) {
isRecording = false;
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
}