Code example for AudioTrack

Methods: playsetNotificationMarkerPositionwrite, setPlaybackPositionUpdateListener

0
     
     
    // Actually plays the sound 
    void playSound(){ 
    	// I should really set the volume at this point 
    	final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, 
				 AudioFormat.CHANNEL_CONFIGURATION_MONO, 
				 AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
				 AudioTrack.MODE_STATIC);
    	audioTrack.write(generatedSnd, 0, generatedSnd.length);
    	audioTrack.setPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener() {
    		@Override 
    		public void onPeriodicNotification(AudioTrack t){
    			// This is to notify when multiples of the notification period? 
    		} 
    		 
    		@Override 
    		public void onMarkerReached(AudioTrack t){
    			// Called when the marker is hit 
    			singleTest(); 
    			Log.d("main:playSound", "ReachedMarker");
    		} 
    	}); 
    	// I can't figure out why this number is a good spot for the marker. 
    	// I can't figure out how many frames there are! 
    	if (testing){
    		audioTrack.setNotificationMarkerPosition((generatedSnd.length/2) - 1000);
    	} 
    	audioTrack.play();
    } 
}