im using clips to play .wav files on raspberry pi device, I checked that on windows laptop and was working without issue, but when I run the game in raspberry, after a time or after play like 6-times the sounds, I got an error of LineException, I checked the state of the clip and every time im receiving those exceptions the clip is closed, I would like to know how to prevent that, the code im using is the next one:
on branch develop, and im also using the first play method. As I said work in windows but not all the time on RPI, Im gonna check this week on Linux mint and macos
I think I can actually answer this one correctly! As I had the same problem. It seems like Windows has unlimited lines (or as many as you would ever need). However, the PI only has 8 lines. For me that’s why it was throwing the LineUnavailableException, because I was trying to open more lines than the PI had. Here is a thread I started about it:
I got around the issue by using TinySound, which is 95% easier than using JavaSound anyway, and it sends all the sound output through the same lines (so basically only 2, for left and right), regardless of how many different sounds you’re using. Also, @philfrei will probably be able to help you out as well, as he is building an awesome sound tool.
The code looks fine, so I’m guessing you ran into the 8-output-line limit on PI. You might try closing any sound that is not being used. (Close both the AudioInputStream and the Clip.)
TinySound is a good solution. This library mixes all sounds into a single output line.
The code I wrote is called AudioCue. It has a smaller footprint than TinySound, and you can do some extra things, like real-time pitch variation, volume fading and panning. You’ll probably only be able to load 8 AudioCues at one time, but the way the class is made, you can play as many instances of those sounds (with pitch variations) as your cpu can handle. In other words, 5 gunshot bullets made and heard at the same time 9rapid succession) will only count as a single output.
I’m working on (and off) on figuring out how to create an option for mixing AudioCue’s together, letting them share an output line. It is a little tricky. I could hack something out in a day, but I’m trying to get the API to make sense rather than have it be kind of cloojy. Juggling a lot of things at the moment and might not figure it out for a while.
i am totally interested on this, i was about to implement tinySound but yesterday i asked to the creator if he has that jar on maven repository to add that dependency to gradle, i didnt know about those 8 lines but now that u are telling me that it makes sense, so are u using RPI to run the games there, because im using pi4j as well to create some kind of control, with a protoboard and that stuff, how can i send private messages, tried before with no luck at all.
Your first play method is opening a new Clip every time you want to play a sound, even if it’s the same file? You’re also never closing the Clips after playback. Basically, this code is all wrong, and you’ll eventually run out of resources on any platform, just much quicker on the Pi.
I think so, I thougth that when a clip reaches the end of the line it states changes to STOP, so I tought they were closed alone, and because I was using local variables, do you have any idea of what can I do properly?
However, if you just have a few sounds / don’t want the hassle of a proper sound system, make sure you only have one Clip per sound file - you seem to have a Map set up for this already? Then just replay the existing Clip, probably rewinding it each time (frame position). You’ll probably still be limited to the number of available sounds you can have on the Pi though.
If the limits on number of sounds is still a problem after this approach, then you’re either going to have to start using listeners to close the clips when they’ve finished playing, or decide that really isn’t worth the effort and pick one of the multiple libraries that can do software mixing (TinySound, Minim and JSyn being obvious ones that have a usable license)
ajjaja tnx man, I was about to implement that way but I was not aware of many things that’s I came with that naïve approach I had, yesterday implemented TinySound, I have to check if that works on RPI.
When I saw that the start method call that is at the end of the play method in the code was commented out, I forgot that there was still a start method call embedded in with the load and open sections. So nsigma is right and I blew it by saying “the code looks fine.” Combining a Clip load, open and start in one code block is likely always the wrong way to do things.
If you only need 8 different sounds, load and open each and hold them as ready to go as instance variables or something. Then you can use the start() method call on an as-needed basis and the sound will play with a minimum of latency.
A clip does not close when it reaches the end of a playback. You can take that same clip, and replay it by setting the framePosition to 0 and then calling start again.
Garbage collection for Line (Clip extends Line) is different than other classes. “Resources” obtained by the line need to be closed before the instance is garbage-collected. If you are done with a Clip, it is important to close it. Stopping and Closing are not the same!
The Sound Tutorial that Oracle provides is not very clear at all on demonstrating simple use cases. Lots of people get messed up and discouraged trying to make sound work as a result.
i tried TinySound and is working well, i checked on Windows and Mac OS X, but wen i ran the game in RPI, it sound the effect but with a delay i mean it does not sound when i hit the enemy but later like a second later, i dont know if that is normal or if i have to configure something else.