【发布时间】:2010-03-16 20:55:38
【问题描述】:
我正在尝试使用 AudioTrack 播放音频缓冲声音 (.wav)。请看下面的代码。我需要在一个线程下调用这个函数来支持同时播放。在线程下很好。正常播放声音工作正常。但是,如果我连续使用 AudioTrack 执行播放声音(即在完成第一个播放声音之前执行第二次播放),则会产生设备崩溃(强制关闭意外错误)。 有没有人遇到过这样的问题并以某种方式解决?
private void PlayAudioTrack(String filePath) throws IOException
{
if (filePath==null)
return;
byte[] byteData = null;
File file = null;
file = new File(filePath); // sdcard path
byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
at.play();
at.write(byteData, 0, byteData.length);
at.stop();
}
感谢您的回复。
谢谢。
【问题讨论】:
标签: android