我正在尝试在我的Android应用程序中同时进行音频录制和播放,该代码第一次正常工作然后崩溃了。给出以下错误。任何人都可以解决这个问题或建议我解决这个问题的方法。
Error :
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40022178 self=0xce48
| sysTid=3508 nice=-19 sched=0/0 cgrp=[fopen-error:2] handle=-1345006496
| schedstat=( 94146739 97717292 625 )
at android.media.AudioTrack.native_write_byte(Native Method)
at android.media.AudioTrack.write(AudioTrack.java:912)
at com.brcm.factorymode.audioloop.speaker.RecordNPlay.loopback(RecordNPlay.java:102)
at com.brcm.factorymode.audioloop.speaker.RecordNPlay$1.run(RecordNPlay.java:131)
at com.brcm.factorymode.audioloop.speaker.RecordNPlay.do_loopback(RecordNPlay.java:136)
at com.brcm.factorymode.audioloop.speaker.RecordNPlay.onCreate(RecordNPlay.java:53)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
请检查以下使用的代码建议。
public void loopback() {
buffer = new byte[BUF_SIZE];
// Prepare the AudioRecord & AudioTrack
Log.e("LOG", "Entered into loopback function....");
try {
buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE,AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
} catch (Throwable t) {
Log.e("Audio", "Buffer size failed");
}
if (buffersize <= BUF_SIZE) {
buffersize = BUF_SIZE;
}
try {
//findAudioRecord();
m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffersize * 1);
m_track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffersize * 1,
AudioTrack.MODE_STREAM);
m_track.setPlaybackRate(SAMPLE_RATE);
Log.e("LOG", "INITIALIZATIONS DONE....");
} catch (Throwable t) {
Log.e("Audio", "Audio Failed");
}
if(m_record!=null)
if (m_record.getState() == AudioRecord.STATE_INITIALIZED)
m_record.startRecording();
if(m_track!=null)
if (m_track.getState() == AudioTrack.STATE_INITIALIZED)
m_track.play();
try {
int i = 0;
while (i<=10) {
// buffer=new byte[buffersize];
m_record.read(buffer, 0, buffersize);
m_track.write(buffer, 0, buffersize);
//m_track.flush();
/* try{
Thread.sleep(200);
}catch(Exception e){}
*/
i++;
}
if (i>10) {
buffer = null;
m_record.stop();
m_track.stop();
i=0;
}
} catch (Exception e) {
Log.e("Em_recordRROR", "exception: " + e.getLocalizedMessage());
}
Log.i("D2Record", "loopback exit");
}
private void do_loopback() {
m_thread = new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
m_isRun = true;
loopback();
}
};
m_thread.run();
}
答案 0 :(得分:0)
此问题已解决,问题出在设备源代码中,我在许多其他设备上对其进行了测试。它工作正常。