I'm trying to run audio in a different thread but I have no idea why but when I call wait(), my entire program seems to wait or hang.. not sure if it's deadlock since all it does is wait(), though if I set a time period, after that period it'll work again meaning it isn't running on a different thread? since my main thread stops when I tell the other thread to wait.. I've been searching the net without any results.. read on synchronization, locks, deadlocks(wondering if this is the problem) but to no avail...

nothing has helped so far at least.. since it's waiting for a notify from the main thread using the lock object(which is synchronized with it), but since the main thread seems to wait and not work with that thread... well.. you can see it's stuck..

code for the thread is

class play implements Runnable
	{
		public synchronized void run()
		{
			synchronized(lock)
			{
				try{      
					AudioInputStream stream = AudioSystem.getAudioInputStream(ClassLoader.getSystemClassLoader().getResource("ssbgm.wav"));      
					DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());      
					Clip clip = (Clip) AudioSystem.getLine(info);         
					 clip.open(stream);
					 clip.start();
					 this.wait();
					 clip.close();
				} 
				catch (Exception e)
				{      
				 	e.printStackTrace();    
				}
			}
		}
	}

and the main is connected to a mouse listener in a customized JButton class in a JPanel object in a JFrame Window that main creates.. then it calls that thread and supposedly, when I click the JButton, it will notify that thread

though how do I make it lock again since the lock gets released when I call wait, just curious since I can just use this.wait for a synchronized thread but just wondering how to make an object a lock again

PPS: any good way for playing audio files(preferably mp3) in java? the ones scattered on the net use the sun media player and audiostream objects if I remember correctly, which have access restrictions placed on them so I can't use them. Also, everything I found on the net about audio streaming is rather.. confusing with some methods contradicting others.. no idea what to do.. and my method.... sucks.. bad... not only does .wav multiply my filesize by God knows how many times.. but the buffer size is so freakin small... had to convert a 1:37 192kbps stereo mp3 to a 8kpbs 11000hz mono wav just so it would fit the buffer... and it sounds absolutely horrible.. so any help with that would be greatly.. greatly appreciated as well...

PPS: Sorry if it's super noob coding.. I just started really learning java 2 days ago, at least I caught on to animation and timers at least(have a timer that paints all objects in a vector, objects are instances of a class I made that stores all sprites and animations in arrays that are easily incremented and looped by the timer, works well with motion as well and the overhead isn't so bad). Anyways, sorry if I'm doing something really stupid since.. nothing I find on the net really helps.. and sorry for wasting your time... thanks in advance :D

First of all "run" should not be synchronized. And, if you synch on "lock", you should also "wait" on "lock", not "this". See the tutorials (and API docs).

http://download.oracle.com/javase/6/docs/

oh well, I tried that too, I tried a lot of combinations.. that was just the most recent.. I was desparate... Still didn't work, and lock released its being synchronized with the thread since when I set the wait to 2000 and it finished, I tried running the lock.notify(); and got the illegalmonitor exception thing.. so it still doesn't really work... and that's also why I was asking how to make lock a lock to that thread again.. since it releases control when it waits(well, I originally heard it in some forum somewhere else), and that was why I tried synchronizing run(also tried adding another synchronized statement

synchronize(this)
{
}

outside the synchronize(lock), and I think you get the idea.. sorry for not mentioning that

Oh yeah, I do go there too, though I usually look through lots of other guides as well, but that helped me through a lot, especially with understanding JFrames and JLayeredPanes.. though I'm a lot more used to C++(though I do love overloading strings in java, makes file processing much easier at least :) )

Well, you need to study threading, in general, much more, before continuing here as this stuff is so far off-track that I can't even decipher what it was you were attempting.

uhm... it plays a sound, and then it will wait for something.. I'm actually going to make it wait until song finishes or user stops it.. my only problem is that when my thread does the wait(), my entire program seems to do it, since it doesnt do anything past bgmthread.run(), meaning it's stuck waiting for it even though it's supposed to run independently from the main function...

though after you declare a runnable class, you just have to make a new Thread and access it's run() right? but don't know why when it does the wait(), my entire program seems to get caught in it.. and I'm wondering what might have caused it..

Holy crap... now I feel like a real blockhead...

my mistake was I called bgmthread.run() instead of bgmthread.start()....

nevermind, sorry to whoever read this thread.. that was just retarded...

found it out because did a debug and found out it never created a new thread(what I suspected), though... never knew it was that...

seriously.. sorry to everyone for wasting your time..

btw masijade, thanks a lot still, read a lot more and learned quite a bit more... sorry for this, you must've facepalmed right now and I'm just very sorry... at least now I learned to implement locks properly, real locks.. though don't have a specific use for it yet... thanks for being patient anyway, bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.