Using AudioTrack in AudioTrack.MODE_STATIC?

南笙酒味 提交于 2019-12-24 14:56:22

问题


I've found lots of tutorials and posts showing how to use AudioTrack to play wav files in AudioTrack.MODE_STREAM and I've successfully implemented this example.

However I'm having issues with performance when playing multiple audio tracks at once and thinking that I should first create the tracks using AudioTrack.MODE_STATIC then just call play each time.

I can't find any resources on how to implement this. How can I do this?

Thanks


回答1:


The two main sticking points for me were realizing that .write() comes first and that the instantiated player must have the size of the entire clip as the buffer_size_in_bytes. Assuming you have recorded a PCM file using AudioRecord, you can play it back with STATIC_MODE like so...

File file = new File(FILENAME);
int audioLength = (int)file.length();
byte filedata[] = new byte[audioLength];
try{
    InputStream inputStream = new BufferedInputStream(new FileInputStream(FILENAME));
    int lengthOfAudioClip = inputStream.read(filedata, 0, audioLength);
    player = new AudioTrack(STREAM_TYPE, SAMPLE_RATE, CHANNEL_OUT_CONFIG, AUDIO_FORMAT,audioLength, AUDIO_MODE);
    player.write(filedata, OFFSET, lengthOfAudioClip);
    player.setPlaybackRate(playbackRate);
    player.play();
 }


来源:https://stackoverflow.com/questions/18624897/using-audiotrack-in-audiotrack-mode-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!