我在播放聲音的開始和結束處(從SD卡開始播放wav)獲得點擊。它必須是跟蹤緩衝區,但我不知道解決方案。另外,每當聲音播放時我都會創建一個新的,這是好的還是有更好的方法?有很多聲音播放一遍又一遍。下面是代碼:Android AudioTrack在開始和結束聲音時點擊
public void PlayAudioTrack(final String filePath, final Float f) throws IOException
{
new Thread(new Runnable() { public void run() {
//play sound here
int minSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,
minSize, AudioTrack.MODE_STREAM);
track.setPlaybackRate((int) (44100*f));
if (filePath==null)
return;
int count = 512 * 1024;
//Read the file..
byte[] byteData = null;
File file = null;
file = new File(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();
while (bytesread < size) {
try {
ret = in.read(byteData,0, count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
track.play();
if (ret != -1) {
// Write the byte array to the track
track.write(byteData,0, ret); bytesread += ret;
}
else break; }
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} track.stop(); track.release();
}
}).start();
}
非常感謝所有幫助
我的音頻體驗不在Android上,但在寫入任何字節之前調用track.play()似乎很奇怪。你不應該先寫字節嗎? – AShelly
不,你需要用play()打開audiotrack然後寫入它,因爲它的mode_streaming。我認爲它可能不會閱讀wav標題的權利或某事。 – user1033558
wtf你認爲你在做ChrisWue嗎?編輯隨機帖子獲得徽章對任何人都不是很有幫助嗎?你至少可以試着回答...... – user1033558