Android: AudioTrack fails simultaneously

I am trying to play audio buffered sound (.wav) using AudioTrack. See code below. I need to call this function in a thread in order to support simultaneous play. It's okay to be under the Thread. It works normally, reproducing sound. But if I play audio using AudioTrack one after the other continuously (i.e., performing a second playback before completing the first audio playback), the device fails (unexpectedly slows down). Does anyone encounter such problems and solve them somehow?

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(); } 

Rate your answer.

Thanks.

+4
source share
1 answer

You need to free AudioTracks resources and also stop it

 at.release(); 
+5
source

Source: https://habr.com/ru/post/908084/


All Articles