我使用以下代码记录麦克风的数据,然后播放。我已经知道缓冲区大小必须匹配。问题是在记录之后,没有播放任何内容。相反,我在日志上收到了这样的消息:
10-07 00:12:09.187:WARN / AudioTrack(3719):obtainBuffer()曲目 0x1df1a8禁用,重启10-07 00:12:10.351: WARN / AudioTrack(3719):gainBuffer()轨道0x1df1a8禁用, 重新启动
这是代码。我做错了什么?
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.w("DEBUG", "Audio record");
int frequency = 44100;
int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
AudioRecord audioRecord = null;
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
Log.w("DEBUG", "Buffer size: " + bufferSize);
//capture data and record to file
int readBytes = 0, writtenBytes = 0;
try
{
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration,
audioEncoding, bufferSize);
// audioRecord = findAudioRecord();
short[] buffer = new short[bufferSize];
byte[] data = new byte[bufferSize];
if (audioRecord == null || (audioRecord != null && audioRecord.getState() != AudioRecord.STATE_INITIALIZED))
{
Log.w("DEBUG", "Can't start");
//Log.w("DEBUG", "status: " + audioRecord.getState());
return;
}
audioRecord.startRecording();
int sampleNumber = 0;
// while (sampleNumber < 30)
{
readBytes = audioRecord.read(buffer, 0, bufferSize);
if (AudioRecord.ERROR_INVALID_OPERATION != readBytes)
{
Log.w("DEBUG", "Writing");
writtenBytes += audioPlayer.write(data, 0, readBytes);
}
Log.w("DEBUG", "Sample number" + sampleNumber);
sampleNumber++;
}
if (audioPlayer.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
{
Log.w("DEBUG", "Playing");
audioPlayer.play();
}
} catch (IllegalArgumentException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalStateException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally
{
if (audioRecord != null && audioRecord.getState() == AudioRecord.STATE_INITIALIZED)
{
audioRecord.stop();
audioRecord.release();
}
}
}
答案 0 :(得分:1)
手机的音量不够高,而且我没有接近麦克风说话:)回声仍然是一个问题,但至少我终于可以听到声音了。