Code example for AudioTrack

Methods: flushgetStatepauserelease

0
	 */ 
	public static void deactivate(PApplet parent) {
		if (audiotrk != null) {
			if (isAudioTrackInitialized()) { 
				//audiotrk.stop(); Maybe this is causing the problem with illegalstateexception 
				audiotrk.pause();
				audiotrk.flush();
			} 
			audiotrk.release();
		} 
		audioControl.relinquishAudioControl(parent);
		audioControl.setCallsOn(parent);
	} 
	 
	public static void stop(PApplet parent) {
		if (audiotrk != null) {
			if (isAudioTrackInitialized()) { 
				audiotrk.stop();
			} 
	} 
	} 
 
	public static boolean isPlaying() { 
		try { 
			return audiotrk.getPlaybackHeadPosition() < (generatedSnd.length);
		} catch (Exception e) {
			return false; 
		} 
	} 
 
	private static int minbufsize;
	private static int length;
 
	private static void playSound() { 
		if (audiotrk != null && isAudioTrackInitialized()) {
			if (generatedSnd != null) {
				while (audiotrk.getPlaybackHeadPosition() < (generatedSnd.length))
					SystemClock.sleep(50); // let existing sample finish first:
											// this can probably be set to a 
											// smarter number using the 
											// information above 
			} 
			if (isAudioTrackInitialized()) { 
				// anything else will fail in obtaining the audiobuffer 
				audiotrk.pause();
				audiotrk.reloadStaticData();
				audiotrk.flush();
 
				// audiotrk.release(); 
			} 
		} 
		UpdateParameters(false); // might as well do it at every iteration, it's 
									// cheap 
		generatedSnd = playque.poll();
		length = generatedSnd.length;
		if (minbufsize < length)
			minbufsize = length;
 
		audiotrk.setStereoVolume(1, 1);
		//System.out.println(length); 
		audiotrk.write(generatedSnd, 0, length);
 
		if (isAudioTrackInitialized()) { 
			audiotrk.play();
		} 
	} 
 
	private static boolean isAudioTrackInitialized() { 
		return (audiotrk.getState() == AudioTrack.STATE_INITIALIZED);
	} 
}