Threads in java module

Monkey Targets Forums/Android/Threads in java module

Artem Kuchin(Posted 2014) [#1]
Sorry for shooting questions like this, but i am new to java and to monkey and to android development and putting java and monkey together isn't easy.

So, here is code from .java

class AKAudio{
	BBAndroidGame game;
	int cc=0;
	Thread t;
	int sr = 44100;
	boolean isRunning=true;
	AudioTrack audioTrack=null;

	int Play(){
		t = new Thread() {
			public void run() {
				// set process priority
//				setPriority(Thread.MAX_PRIORITY);
				// set the buffer size
				int buffsize = AudioTrack.getMinBufferSize(sr, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
				// create an audiotrack object
				audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sr, AudioFormat.CHANNEL_OUT_MONO,
                                  AudioFormat.ENCODING_PCM_16BIT, buffsize,
                                  AudioTrack.MODE_STREAM);

				short samples[] = new short[buffsize];
				int amp = 10000;
				double twopi = 8.*Math.atan(1.);
				double fr = 440.f;
				double ph = 0.0;

				// start audio
				audioTrack.play();

				// synthesis loop
				while(isRunning){
					fr =  440 + 440;
					for(int i=0; i < buffsize; i++){
						samples[i] = (short) (amp*Math.sin(ph));
						ph += twopi*fr/sr;
					}
					audioTrack.write(samples, 0, buffsize);
				}	
			}
		};
		return 1;
	}
	
	int getPlaybackHeadPosition(){
		return audioTrack.getPlaybackHeadPosition();
	}
	int Stop(){
		audioTrack.stop();
		audioTrack.release();
		t=null;
		return 1;
	}
	

}


Nothing fancy, just a copy paste sin wave generator.
The problem is that when i run it it does not generate sound.
If i comment out thread (do not use thread) then it does play tone but application of course blocks and do not redraw.
Also, if i call getPlaybackHeadPosition the app exits.

Monkey code

Import mojo
Import AK


Class Test Extends App
	Field a:AKAudio

	Method OnCreate ()
		SetUpdateRate 10
		a=New AKAudio
		a.Play();
	End

	Method OnUpdate ()

	End

	Method OnRender ()
		Cls 32, 64, 128						
		SetColor 255,255,255
		DrawText a.getPlaybackHeadPosition(),0,0
	End

End


Function Main ()
	New Test
End



So, what's wrong with using thread here?

I run it on bluestack and on my phone. Bluestack silently exists. Phone says: application is stopped.

When i start in emulator i see

E/AndroidRuntime( 961): FATAL EXCEPTION: GLThread 60
E/AndroidRuntime( 961): Process: com.monkeycoder.monkeygame, PID: 961
E/AndroidRuntime( 961): java.lang.NullPointerException
E/AndroidRuntime( 961): at com.monkeycoder.monkeygame.AKAudio.getPlaybackHeadPosition(MonkeyGame.java:2722)
E/AndroidRuntime( 961): at com.monkeycoder.monkeygame.c_AKAudio.p_getPlaybackHeadPosition(MonkeyGame.java:3476)
E/AndroidRuntime( 961): at com.monkeycoder.monkeygame.c_Test.p_OnRender(MonkeyGame.java:2829)
E/AndroidRuntime( 961): at com.monkeycoder.monkeygame.c_GameDelegate.RenderGame(MonkeyGame.java:2905)
E/AndroidRuntime( 961): at com.monkeycoder.monkeygame.BBGame.RenderGame(MonkeyGame.java:571)
E/AndroidRuntime( 961): at com.monkeycoder.monkeygame.BBAndroidGame.onDrawFrame(MonkeyGame.java:1307)
E/AndroidRuntime( 961): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
E/AndroidRuntime( 961): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

And why the heck is there a NULL pointer?


therevills(Posted 2014) [#2]
"audioTrack" must be null, maybe wrap it:
int getPlaybackHeadPosition(){
	if (audioTrack==null)
		return 0;
	else
		return audioTrack.getPlaybackHeadPosition();
}



Artem Kuchin(Posted 2014) [#3]
yes, it is null, but this is not the cause of the problem. It is just a way the problem shows itself.

But i solved it anyway this morning. Some sleep always helps.

If you notice , i forgot to start the thread :)
Adding t.start() in Play() solved the problem.
Now everything works.


therevills(Posted 2014) [#4]
Haha - yeah I didn't see that! I had a similar issue when I was coding the AdMob full screen stuff..