android - audioTrack.stop doesn't stop playing audio -


i creating audiotrack following definition.

audiotrack = new audiotrack(                             audiomanager.stream_music,                             44100,                             audioformat.channel_out_mono,                             audioformat.encoding_pcm_16bit,                             buffer.length * 2,                             audiotrack.mode_static);  audiotrack.write(buffer, 0, buffer.length);                     audiotrack.setpositionnotificationperiod(500);                     audiotrack.setnotificationmarkerposition(buffer.length);                     progresslistener = new playbackprogress(buffer.length);                     audiotrack.setplaybackpositionupdatelistener(progresslistener); 

when audiotrack finishes, following called stop audio , reset head position.

private void resetaudioplayback() {         viewgroup.layoutparams params = playbackview.getlayoutparams();         params.width = 0;         playbackview.setlayoutparams(params);         audiotrack.stop();         audiotrack.reloadstaticdata();         playimage.animate().alpha(100).setduration(500).start();     } 

the above code works fine android 5.1. having issues 4.4.4. audiotrack.stop() called audio not stopped, since reloadstaticdata rewinds audio start position, replays audio. 5.1, correctly stops , resets buffer start of playback , when play button pressed, plays beginning.

can me how can issue android 4.4.4?

i'm not absolutely if solve problem, consider using pause() instead of stop(). documentation, stop() mode_stream keep playing remainder of last buffer written. you're using mode_static, might worth trying.

also (possibly unrelated), consider write() returns number of bytes written, shouldn't depend on single write filling entire buffer of audiotrack every time. write() should treated outputstream write in may not write entire contents of buffer given, it's better write loop , check how has been written each call write(), continue write new index in buffer array until sum of writes equals length of buffer.


Comments