question: public void run ( ) { Log . d ( LOG , "run(): sampleRate=" + sampleRate + ", channels=" + channels + ", bufferSizeInBytes=" + bufferSizeInBytes + " (" + bufferSizeInMs + " ms)" ) ; isPlaying = false ; AudioTrack atrack = null ; try { atrack = new AudioTrack ( AudioManager . STREAM_MUSIC , sampleRate , channels == 1 ? AudioFormat . CHANNEL_CONFIGURATION_MONO : AudioFormat . CHANNEL_CONFIGURATION_STEREO , AudioFormat . ENCODING_PCM_16BIT , bufferSizeInBytes , AudioTrack . MODE_STREAM ) ; atrack . setPlaybackPositionUpdateListener ( this ) ; atrack . setPositionNotificationPeriod ( msToSamples ( 200 , sampleRate , channels ) ) ; if ( playerCallback != null ) playerCallback . playerAudioTrackCreated ( atrack ) ; this . audioTrack = atrack ; } catch ( Throwable t ) { Log . e ( LOG , "Cannot create AudioTrack: " + t ) ; stop ( ) ; if ( playerCallback != null ) playerCallback . playerException ( t ) ; } while ( ! stopped ) { int ln = acquireSamples ( ) ; if ( stopped || ln == 0 ) { releaseSamples ( ) ; break ; } int writtenNow = 0 ; do { if ( writtenNow != 0 ) { Log . d ( LOG , "too fast for playback, sleeping..." ) ; try { Thread . sleep ( 50 ) ; } catch ( InterruptedException e ) { } } int written = atrack . write ( lsamples , writtenNow , ln ) ; if ( written < 0 ) { Log . e ( LOG , "error in playback feed: " + written ) ; stopped = true ; break ; } writtenTotal += written ; int buffered = writtenTotal - atrack . getPlaybackHeadPosition ( ) * channels ; if ( ! stopped && ! isPlaying ) { if ( buffered * 2 >= bufferSizeInBytes ) { Log . d ( LOG , "start of AudioTrack - buffered " + buffered + " samples" ) ; atrack . play ( ) ; isPlaying = true ; } else { Log . d ( LOG , "start buffer not filled enough - AudioTrack not started yet" ) ; } } writtenNow += written ; ln -= written ; } while ( ln > 0 && ! stopped ) ; releaseSamples ( ) ; } if ( ! stopped && stoppedByEOF ) waitForLastTone ( ) ; if ( isPlaying ) atrack . pause ( ) ; atrack . flush ( ) ; atrack . release ( ) ; stopped = true ; Log . d ( LOG , "run() stopped." ) ; }
target:  The main execution loop which should be executed in its own thread.