某些三星手机的音频驱动好像是改过的,在 HTC 手机上跑的好好的程序,拿到三星上就录音崩溃。我目前的手机是三星GT-I8150,我的源码如下:
public class AndroidAudioRecordManager
{
public int frequency =8000;
public int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int recBufSize, playBufSize;
AudioRecord audioRecord;
boolean isRecording = false;
private long filterCtxPtr = 0;
Thread recordThread=null;

private native void putAudioData(long filterCtxPtr, byte[] buffer,
int dataLen); public void startParametersFromFilter(long filterCtxPtr, int rate,
int channel, int bits)
{
this.filterCtxPtr = filterCtxPtr;
frequency = rate;
recBufSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration, audioEncoding,
recBufSize);
isRecording = true;
recordThread=new AudioRecordThread();
recordThread.start();
} public void stopFromFilter()
{
isRecording = false;
try
{
recordThread.join();
} catch (InterruptedException e)
{
}
} class AudioRecordThread extends Thread
{
public void run()
{
android.os.Process.setThreadPriority(-19);
try
{
byte[] buffer = new byte[recBufSize];
int rstat=audioRecord.getRecordingState();
int dstat=audioRecord.getState();
if(dstat!=AudioRecord.STATE_INITIALIZED)
{
return;
}
Thread.sleep(1000);
audioRecord.startRecording();
while(isRecording)
{
int bufferReadResult = audioRecord.read(buffer, 0, 320);
putAudioData(filterCtxPtr, buffer, bufferReadResult);
}
audioRecord.stop();
} catch (Throwable t)
{
}
finally
{
audioRecord.release();
audioRecord=null;
}
}
}
};执行到 int bufferReadResult = audioRecord.read(buffer, 0, 320); 这一句,bufferReadResult 始终是 -110。请有经验的大虾帮帮忙,高分相赠,非常感谢!

解决方案 »

  1.   

    sampleRateInHz  the sample rate expressed in Hertz. 44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices. api说明,把采样率设置为44100hz试试。
      

  2.   

    logcat有没有报错?会不会和线程有关?
      

  3.   

    3 楼:
    在其他手机上没有问题。4 楼:
    系统录音并不是 PCM 数据,我需要 PCM 数据,而且我需要实时数据,而不是声音文件。
      

  4.   

    楼主,我遇到了recorder初始化失败的问题,帮个忙,怎么解决呀