After creating a player by using method this code:
player = Manager.createPlayer(getClass().getInputStream("/sound.wav"), "audio/x-wav")
Is it possible to pass an audio file to same instance of player?
I tried by doing this:
player = (Player) player.getClass().getInputStream("/sound.wav");
but this one give me an exception which is java.lang.ClassCastException
If its is possible then how it should be done?
This line
gives you a
ClassCastExceptionbecause you are trying to cast anInputStreaminto aPlayer. That's wrong. This line correctly assigns aPlayerto yourplayervariable:If you want, you can use my second line again and again, when it's time to replay the sound. You will just be recreating the player. If you are not changing the sound file (.wav), then when the sound finishes playing, I would just start it again without creating a new player:
But, if you are changing which sound file you use, it might be easier just to call
createPlayeragain:Here are some BlackBerry docs on the Player. Even though you may be on a different J2ME platform, I think the same lifecycle rules probably apply:
Update: as you seem to be concerned with code complexity, and perhaps deallocating (?) objects, here is a code sample, that hopefully illustrates that it shouldn't be too difficult. You would just take the code in that answer, and make it a method where you pass in the name of the autio file (
musicFile).