我需要你的帮助。我正在尝试用Androids AudioRecord类录制一些音频。 大多数时候这种方法效果很好,但有时候会出现 SIGSEGV 错误。
以下是在录制音频时在单独的线程中运行的代码:
private void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
@Override
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
private void writeAudioDataToFile() {
byte data[] = new byte[bufferSize];
String filename = getTempFilename();
FileOutputStream os = null;
try {
os = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int read = 0;
if (null != os) {
while (isRecording) {
read = recorder.read(data, 0, bufferSize);
if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
os.close();
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当录音停止时,我就是这样做的。
public void stopRecording() {
if (null != recorder) {
isRecording = false;
mRecordingBlocked = true;
mProgressDialog = RecorderDialog.newInstance();
mProgressDialog.show(getFragmentManager(), "progressDialog");
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
new Thread(new Runnable() {
@Override
public void run() {
String fileName = getFilename();
copyWaveFile(getTempFilename(), fileName);
final TelephonyManager tm = (TelephonyManager) getActivity().getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
String deviceId = deviceUuid.toString();
App.db.insertEntry(deviceId, "Song title", fileName);
deleteTempFile();
if (cbox_overdub.isChecked()) {
phrases.add(playAudio(fileName, true));
}
}
}).start();
}
}
private void copyWaveFile(String inFilename, String outFilename) {
FileInputStream in;
FileOutputStream out;
long totalAudioLen = 0;
long totalDataLen;
long longSampleRate = RECORDER_SAMPLERATE;
int channels = 2;
long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;
try {
AppLog.logString("begin copyWaveFile try{}");
File file = new File(inFilename);
in = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;
in.read(bytes);
in.close();
out = new FileOutputStream(outFilename);
AppLog.logString("fos created");
WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
longSampleRate, channels, byteRate);
AppLog.logString("wave header written");
int bufferLength = 1024;
publishProgress(0);
for (int i = 0; i < bytes.length; i += bufferLength) {
int progress = (int) ((i / (float) bytes.length) * 100);
publishProgress(progress);
if (bytes.length - i >= bufferLength) {
out.write(bytes, i, bufferLength);
} else {
out.write(bytes, i, bytes.length - i);
}
}
publishProgress(100);
AppLog.logString("progress complete");
mRecordingBlocked = false;
mProgressDialog.dismiss();
out.close();
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
似乎在AudioRecord实例已经停止后发生此错误。然后我将临时temp_record.wav(SD卡)的内容复制到目的地(SD卡),并设置适当的波形文件头。
我无法从以下调试信息中过滤掉任何有用的信息。我希望有人有个主意。谢谢!
这是Logcat输出:
06-30 14:47:53.311: INFO/DEBUG(9381): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-30 14:47:53.311: INFO/DEBUG(9381): Build fingerprint: 'otorola/RTGB/umts_milestone2:2.3.4/MILS2_U6_4.1-22/1317097892:user/release-keys'
06-30 14:47:53.311: INFO/DEBUG(9381): pid: 9459, tid: 9525 >>> de.intermeco.android.apps.ilaugh <<<
06-30 14:47:53.311: INFO/DEBUG(9381): signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 4640e000
06-30 14:47:53.311: INFO/DEBUG(9381): r0 4640e000 r1 00000000 r2 fffffd49 r3 4670da18
06-30 14:47:53.311: INFO/DEBUG(9381): r4 4640e000 r5 4640e000 r6 fffffd49 r7 4670da18
06-30 14:47:53.311: INFO/DEBUG(9381): r8 a904cf6a r9 0000000a 10 a904cfad fp a904cff0
06-30 14:47:53.311: INFO/DEBUG(9381): ip a9060074 sp 4670d9e0 lr afd11704 pc afd113dc cpsr 20000050
06-30 14:47:53.311: INFO/DEBUG(9381): d0 6472656767756265 d1 0037fff00020000c
06-30 14:47:53.311: INFO/DEBUG(9381): d2 fff5ffd8fff5ffdb d3 ffe0ffd8ffd9ffd8
06-30 14:47:53.311: INFO/DEBUG(9381): d4 fff2ffe50008ffd4 d5 ffdafffafff20000
06-30 14:47:53.311: INFO/DEBUG(9381): d6 fff40003ffcf0003 d7 ffe30013ffe1fffd
06-30 14:47:53.311: INFO/DEBUG(9381): d8 0000000000989680 d9 0000000000000000
06-30 14:47:53.311: INFO/DEBUG(9381): d10 0000000000000000 d11 0000000000000000
06-30 14:47:53.311: INFO/DEBUG(9381): d12 0000000000000000 d13 0000000000000000
06-30 14:47:53.311: INFO/DEBUG(9381): d14 0000000000000000 d15 0000000000000000
06-30 14:47:53.311: INFO/DEBUG(9381): d16 000000c24003a7e0 d17 4000000000000000
06-30 14:47:53.311: INFO/DEBUG(9381): d18 3ff0000000000000 d19 0000000000000000
06-30 14:47:53.311: INFO/DEBUG(9381): d20 b96377ce858a5d48 d21 3929f5135cb87c55
06-30 14:47:53.311: INFO/DEBUG(9381): d22 3e21ee9ebdb4b1c4 d23 bda8fae9be8838d4
06-30 14:47:53.319: INFO/DEBUG(9381): d24 0000000000000000 d25 0000000000000000
06-30 14:47:53.319: INFO/DEBUG(9381): d26 0000000000000000 d27 ffffffffffffffff
06-30 14:47:53.319: INFO/DEBUG(9381): d28 0100010001000100 d29 0100010001000100
06-30 14:47:53.319: INFO/DEBUG(9381): d30 4086800000000000 d31 3ff0000000000000
06-30 14:47:53.319: INFO/DEBUG(9381): scr 60000010
06-30 14:47:53.483: INFO/DEBUG(9381): #00 pc 000113dc /system/lib/libc.so (pthread_mutex_lock)
06-30 14:47:53.483: INFO/DEBUG(9381): #01 pc 00011700 /system/lib/libc.so (__pthread_cond_timedwait_relative)
06-30 14:47:53.483: INFO/DEBUG(9381): #02 pc 0002d658 /system/lib/libmedia.so
06-30 14:47:53.483: INFO/DEBUG(9381): code around pc:
06-30 14:47:53.483: INFO/DEBUG(9381): afd113bc e3a02001 ebfffe82 e1a00005 e8bd87f0
06-30 14:47:53.483: INFO/DEBUG(9381): afd113cc 00036024 e92d47f0 e2504000 0a000019
06-30 14:47:53.483: INFO/DEBUG(9381): afd113dc e5946000 e5947000 e2166903 1a000017
06-30 14:47:53.483: INFO/DEBUG(9381): afd113ec e5945000 e1a02004 e2055a02 e1a00005
06-30 14:47:53.483: INFO/DEBUG(9381): afd113fc e3851001 ebffed7f e3500000 13856002
06-30 14:47:53.483: INFO/DEBUG(9381): code around lr:
06-30 14:47:53.483: INFO/DEBUG(9381): afd116e4 e1a03007 e1a02006 e2011001 e1a00004
06-30 14:47:53.483: INFO/DEBUG(9381): afd116f4 ebfffd9a e1a04000 e1a00005 ebffff32
06-30 14:47:53.483: INFO/DEBUG(9381): afd11704 e374006e 03a0006e 13a00000 e8bd81f0
06-30 14:47:53.483: INFO/DEBUG(9381): afd11714 e304cdd3 e3043240 e92d4010 e341c062
06-30 14:47:53.483: INFO/DEBUG(9381): afd11724 e1a0e002 e24dd008 e340300f e1a0200d
06-30 14:47:53.483: INFO/DEBUG(9381): stack:
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9a0 04000804
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9a4 00000001
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9a8 001c6b58
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9ac 00000262
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9b0 0000ee6b
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9b4 afd0fe34 /system/lib/libc.so
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9b8 001edbb0
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9bc afd13bc1 /system/lib/libc.so
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9c0 001b0470
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9c4 001c6b58
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9c8 00000241
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9cc 3b9aca00
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9d0 00000000
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9d4 00989680
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9d8 df002777
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9dc e3a070ad
06-30 14:47:53.483: INFO/DEBUG(9381): #00 4670d9e0 00000000
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9e4 4640e000
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9e8 fffffd49
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9ec 4670da18
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9f0 a904cf6a /system/lib/libmedia.so
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9f4 0000000a
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9f8 a904cfad /system/lib/libmedia.so
06-30 14:47:53.483: INFO/DEBUG(9381): 4670d9fc afd11704 /system/lib/libc.so
06-30 14:47:53.483: INFO/DEBUG(9381): #01 4670da00 4640e000
06-30 14:47:53.483: INFO/DEBUG(9381): 4670da04 4640e004
06-30 14:47:53.483: INFO/DEBUG(9381): 4670da08 4670da98
06-30 14:47:53.483: INFO/DEBUG(9381): 4670da0c 000000c8
06-30 14:47:53.483: INFO/DEBUG(9381): 4670da10 a904cf6a /system/lib/libmedia.so
06-30 14:47:53.483: INFO/DEBUG(9381): 4670da14 a902d65b /system/lib/libmedia.so
答案 0 :(得分:3)
感谢再次指出SIGSEV,我读了一下因为我不知道它的意思。现在我做:) 我同意CommonsWare您应该通知制造商/ modder固件。 但我也认为它在一段有用的时间内没有帮助你。也许我们找到一种修改代码的方法,不再引发错误。
通常你的代码看起来很不错。我认为停止录音机的方式并不完全安全。
希望我能帮助一点,你就能找到办法。
答案 1 :(得分:2)
感谢您提出的建议。 我重新编写了一些代码,现在它似乎工作了。 至少我没有得到错误大约一天(比以前更久:D)。
我改变了方法writeAudioDataToFile(),以便现在也在那里发布等等(在stopRecording()方法中将isRecording设置为false之后)。我也不再创建第二个Thread,而是处理RecorderThread中的所有内容。
所以这似乎对我有用:
private void writeAudioDataToFile() {
byte data[] = new byte[bufferSize];
String filename = getTempFilename();
FileOutputStream os = null;
try {
os = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int read = 0;
if (null != os) {
while (isRecording) {
read = recorder.read(data, 0, bufferSize);
if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
os.close();
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
recorder.stop();
recorder.release();
recorder = null;
String fileName = getFilename();
copyWaveFile(getTempFilename(), fileName);
final TelephonyManager tm = (TelephonyManager) getActivity().getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getActivity().getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
String deviceId = deviceUuid.toString();
App.db.insertEntry(deviceId, "Song title", fileName);
deleteTempFile();
if (cbox_overdub.isChecked()) {
phrases.add(playAudio(fileName, true));
}
}
}
public void stopRecording() {
if (null != recorder) {
Toast.makeText(getActivity(), "Stop Recording", Toast.LENGTH_SHORT).show();
((ImageView) getActivity().findViewById(R.id.btnMic)).setImageDrawable(getActivity().getResources().getDrawable(R.drawable.btn_mic));
isRecording = false;
mRecordingBlocked = true;
mProgressDialog = RecorderDialog.newInstance();
mProgressDialog.show(getFragmentManager(), "progressDialog");
}
}
private void publishProgress(int progress) {
ProgressDialog pd = ((ProgressDialog) mProgressDialog.getDialog());
if (pd != null) pd.setProgress(progress);
}
谢谢你!
答案 2 :(得分:0)
Java代码中不应该有SIGSEGV
。除非这是您的JNI代码,否则这将表明固件存在缺陷。
如果您正在运行修改的固件,请将此信息传递给固件模块。
如果您运行的是设备附带的原始固件,而这只发生在一台设备上,您可以尝试联系设备制造商。
如果您在多个具有原始固件的设备上看到此信息,并且您可以创建一个再现错误的示例项目,请在the Android issue tracker上发布项目和堆栈跟踪,假设此问题尚未报告那里。
答案 3 :(得分:0)
文档说明:
为了接收与这些相关的相应回调 监听器,应用程序需要创建MediaRecorder对象 在运行Looper的线程(默认情况下为主UI线程) 已经有一个Looper跑了。)
确保在UI线程上创建记录器。也许也可以在UI线程上调用它的方法。
答案 4 :(得分:0)
以上解决方案均不适合我。相反,我增加了AudioRecord的构造函数中的缓冲区。
最小缓冲区大小建议介于1k和4k之间,具体取决于参数和设备。看来我的算法太慢,无法在缓冲区满之前从缓冲区读取数据,所以我得到了segv(老实说,我只是猜测后果)。
在我将缓冲区增强到两倍于我需要来自AudioRecord的数据的值后,它就像魅力一样。
dfs = []
data = """\
num a
101 0
102 1
103 0
104 0
105 1
106 1
"""
dfs.append(pd.read_csv(io.StringIO(data), delim_whitespace=True))
data = """\
num b
101 1
103 1
104 0
105 0
107 1
108 1
"""
dfs.append(pd.read_csv(io.StringIO(data), delim_whitespace=True))
data = """\
num c
102 0
103 0
104 1
105 1
106 1
107 1
"""
dfs.append(pd.read_csv(io.StringIO(data), delim_whitespace=True))
data = """\
num d
101 1
102 0
103 1
104 1
106 0
107 0
"""
dfs.append(pd.read_csv(io.StringIO(data), delim_whitespace=True))