2

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?

gnat
  • 6,213
  • 108
  • 53
  • 73
Rahul More
  • 615
  • 3
  • 13
  • 41

1 Answers1

1

This line

player = (Player) player.getClass().getInputStream("/sound.wav");

gives you a ClassCastException because you are trying to cast an InputStream into a Player. That's wrong. This line correctly assigns a Player to your player variable:

player = Manager.createPlayer(getClass().getInputStream("/sound.wav"), "audio/x-wav");

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:

player.start();  // restart the same sound

But, if you are changing which sound file you use, it might be easier just to call createPlayer again:

player = Manager.createPlayer(getClass().getInputStream("/another_sound.wav"), "audio/x-wav");

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:

A Player has five states: UNREALIZED, REALIZED, PREFETCHED, STARTED, CLOSED.

The purpose of these life-cycle states is to provide programmatic control over potentially time-consuming operations. For example, when a Player is first constructed, it's in the UNREALIZED state. Transitioned from UNREALIZED to REALIZED, the Player performs the communication necessary to locate all of the resources it needs to function (such as communicating with a server or a file system). The realize method allows an application to initiate this potentially time-consuming process at an appropriate time.

Typically, a Player moves from the UNREALIZED state to the REALIZED state, then to the PREFETCHED state, and finally on to the STARTED state.

A Player stops when it reaches the end of media; or when the stop method is invoked. When that happens, the Player moves from the STARTED state back to the PREFETCHED state. It is then ready to repeat the cycle.

To use a Player, you must set up parameters to manage its movement through these life-cycle states and then move it through the states using the Player's state transition methods.

enter image description here

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).

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • That means, there is no method to pass another audio file without creating player.... I have to create another player. But in my app i am using VolumeControl. If i create another player to play 2nd file previously created VolumeControl wont work with another player... To overcome this problem I would have create an another VolumeControl for 2nd player or deallocating the first one VolumeControl & reassigning them to 2nd player.... This will increase the complexity of code n efforts too... Thats why I want a method to pass an audio file without creating an another player..... – Rahul More Jan 03 '13 at 08:33
  • 2
    There's no additional *complexity* needed. You write one method with 6-10 lines of code. In that method, you create a player with `createPlayer()` and get your `VolumeControl`. You call that one method more than once. You call it when you first play the sound, and again when you want to play it again. There's no additional complexity. Trying to swap sound files *would* create additional complexity. I understand what you want, but you can look at the API docs yourself. I don't see any method to **change** the audio file. If someone else does, hopefully they'll post a solution. – Nate Jan 03 '13 at 08:53
  • Ok Nate..... I understood & my problem is solved now... :D... thanx for the suggestion.. There is one more question in mind that is How do I come to know that what is the capability of emulator or device to play certain media file though it is .mp3 or .wav or any other file – Rahul More Jan 04 '13 at 10:39
  • That's a good question, but it's a **different** question than what you posted here. If this answer solved *this* problem, please **accept** it by clicking the little checkmark ("V") icon next to the answer. Then, post a new question, asking about device media capabilities. This will let other people see your question. Very few people will be able to read it way down here. Thanks! – Nate Jan 04 '13 at 10:42
  • No problem. I *think* I see how to solve the other problem, but it's getting late for me. If you post a new question in the next few minutes, I can probably answer it. If not, I'll check back tomorrow ... – Nate Jan 04 '13 at 10:50