Android AudioTrack错误:未初始化的AudioTrack
问题描述:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
playfilesound();
}
private void playfilesound() throws IOException
{
int count = 512 * 1024; // 512 kb
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"recordsound"); //filePath
byteData = new byte[(int)count];
FileInputStream in = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int bytesread = 0, ret = 0;
int size = (int) file.length();
audioTrack.play();
while (bytesread < size) { // Write the byte array to the track
ret = in.read(byteData,0, count); //ret =size in bytes
if (ret != -1) {
audioTrack.write(byteData,0, ret);
bytesread += ret; } //ret
else break;
} //while
in.close();
audioTrack.stop(); audioTrack.release();
}
我用调试器来遍历代码和悬停信息audioTrack,它的分配和inited。该文件也存在。Android AudioTrack错误:未初始化的AudioTrack
但是,当它击中audioTrack.play()时,它会抛出一个错误,指出其非法状态异常,未使用的AudioTrack。
我附上了包含录音文件部分的项目。 http://www.mediafire.com/?6i2r3whg7e7rs79
答
看起来像你在写作之前称为玩游戏!试试这个...
int bytesread = 0, ret = 0;
int size = (int) file.length();
//audioTrack.play(); <---- play called prematurely
while (bytesread < size) { // Write the byte array to the track
ret = in.read(byteData,0, count); //ret =size in bytes
if (ret != -1) {
audioTrack.write(byteData,0, ret);
bytesread += ret;
audioTrack.play(); //<--- try calling it here!
} //ret
else break;
} //while
答
使用通道的配置被中断,在AudioFormat.CHANNEL_CONFIGURATION_MONO
的地方使用AudioFormat.CHANNEL_IN_MONO
录制并AudioFormat.CHANNEL_OUT_MONO
玩...
请SO使用英语和英语只在这里。 – 2017-02-02 00:25:59
Por支持,utilice elinglésysóloelinglésaquíen SO。 – NobodyNada 2017-02-02 00:26:41