我想知道我的代码遗漏或错误。我阅读了有关AudioRecord的内容并想要应用它,以便我可以使用NoiseSuppression API。好吧,在我的模拟器中,一切正常,PCM文件存储在我指定的路径上,但在我构建APK并在我的设备上运行以进行实际测试之后,只需在录制后强制关闭。也许这是因为线程,但我无法确定它。此外,我无法在设备上阅读我的日志,因为我不知道如何设置它(使用Galaxy Tab 2 7.0)。
此外,我只需稍加调整即可获得我的所有代码参考here。
到目前为止,这是录音机的代码:
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by pc on 8/5/13.
*/
public class Recorder {
private static final int SAMPLE_RATE = 8000;
private static final int AUDIO_CHANNEL = AudioFormat.CHANNEL_IN_MONO;
private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord recorder = null;
int bufferSize = 1024, byteSize = 2;
private State recStatus = State.Initial;
enum State { Initial, Recording, StopRecording };
public void startRecording(){
recordStart();
}
public void stopRecording() {
if (recorder!=null) {
recStatus = State.StopRecording;
recorder.stop();
recorder.release();
recorder = null;
}
}
private void recordStart() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AUDIO_CHANNEL, AUDIO_FORMAT, bufferSize*byteSize);
recorder.startRecording();
recStatus = State.Recording;
/*start the record thread*/
Thread playback = new recordThread();
playback.start();
}
private class recordThread extends Thread{
public void run() {
while(recStatus== Recorder.State.Recording){
writeAudioDataToFile();
}
}
}
private void writeAudioDataToFile() {
// Write the output audio in byte
File record_path = new File(Environment.getExternalStorageDirectory(),"/Echo/Recordings");
String filePath = record_path.getAbsolutePath()+"/"+System.currentTimeMillis()+".pcm";
short sData[] = new short[bufferSize];
if(!record_path.exists()){
record_path.mkdirs(); // make directory if not existing
}
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (recStatus == Recorder.State.Recording) {
// gets the voice output from microphone to byte format
recorder.read(sData, 0, bufferSize);
Log.v("Writing to file", sData.toString());
try {
byte bData[] = short2byte(sData);
os.write(bData, 0, bufferSize * byteSize);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
}
请求日志使用genymotion模拟器(我在这部分找不到任何错误):
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.285 2213-2276/com.marktest.noisesuppression V/Writing to file: [S@a6b161c0
08-05 18:06:10.309 2213-2216/com.marktest.noisesuppression D/dalvikvm: GC_CONCURRENT freed 384K, 9% free 8060K/8775K, paused 11ms+0ms, total 23ms
08-05 18:06:10.309 2213-2280/com.marktest.noisesuppression V/Writing to file: [S@a6b1e3e8
08-05 18:06:14.049 100-279/? W/audio_hw_primary: out_write() limiting sleep time 46802 to 39909
08-05 18:06:14.973 301-328/system_process E/Genymotion: Could not open '/sys/class/power_supply/genymotion_fake_path/present'
08-05 18:06:18.717 100-279/? W/audio_hw_primary: out_write() limiting sleep time 46802 to 39909
08-05 18:06:19.969 301-328/system_process E/Genymotion: Could not open '/sys/class/power_supply/genymotion_fake_path/present'