Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 4
Registered: ‎08-31-2009
My Device: Not Specified

How to play mp3 or wav sounds for a game?

I've read many of the posts here about using a Player to play sounds, but I'm still having trouble.

 

I've created a class that I will use in my game.  I need to be able to play a click sound at certain times, and (depending on the simulator I use), I either get no sounds or a single click sound which never happens again.

 

Here's my code:

 

 

import java.io.InputStream;
import javax.microedition.media.control.*;
import javax.microedition.media.*;

public class Sounds {

Player clickplayer = null;

Sounds()
{
try {
InputStream instream = getClass().getResourceAsStream("/click.mp3");
clickplayer = Manager.createPlayer(instream, "audio/mpeg");
clickplayer.realize();
clickplayer.setLoopCount(1);

VolumeControl vc = (VolumeControl) clickplayer.getControl("VolumeControl");
if (vc != null)
vc.setLevel(100);

clickplayer.prefetch();

}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public void PlayClick()
{
try
{
System.out.println("About to click --------------------------------");
clickplayer.setMediaTime(0);
clickplayer.start();
System.out.println("Click sound sent");
}
catch (Exception e)
{
String mess = e.getMessage();
System.out.println(mess);
}
}
}

 

 I know the MP3 is loaded properly, because when I walk through the code, the stream isn't null.  And, of course with some simulators I get a single click, even though I call sounds.PlayClick() several times.

 

I've tried MP3s and WAV files (and even AU files), and nothing works consistantly.  I want to target JDE 4.2.1 and above.  Is there a way to do this?

 

Thanks,

Gregor

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

 

Once played, your input stream will need to be reset, and your player closed.

 

I only see the input stream being initialized in the constructor.

 

You might want to search the forum a little - I see this question several times per week.

 

 

 

New Developer
Posts: 4
Registered: ‎08-31-2009
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

Right, well there are a number of posts that talk about buffering sounds, which is what this is all about.  Believe me, I've searched through this forum extensively.

 

The goal is to avoid the extensive startup time, so I can load the sounds in the constructor, and then play them in PlayClick.  If I open and shut down the player every time, that seems like it's going to be a little slow...

 

I will try resetting the inputstream, though.  My assumption was that the player loaded the sound and had its own copy.  However, maybe it just uses the stream, so I'd need to reset the stream each time.  That may allow me to keep the player open for better performance.

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

I agree with loading the resource in the constructor - this is a time consuming operation.

 

However, if you do not close the player, your next post will probably be something like "application ends with 'too many threads' error after playing MP3 mutliple times."

 

(been there, done that) Smiley Happy

 

New Developer
Posts: 4
Registered: ‎08-31-2009
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

So, are you saying the only solution is to create a player every time? That buffering like this won't work?

 

In other words, if I create a single player and use it through the game (and close it when the game is done) it will eventually use up too many resources and error out?

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

I have a program that plays sounds occasionally, so the overhead is not a factor. I *did* have an issue with failing to close the player that started occurring in an early OS 4.6 (as I recall). Not certain, but this may have been a bug in the RIM API that was fixed in a later release.

 

I agree that this should be do-able. I think you are going to have to experiment a little to come up with a solution.

 

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

I just reviewed the docs for Player, and I think you are on the right track with your code above.

 

According to the javadoc, you should be able to prefetch and call start() multiple times to play the sound over and over.

 

 

New Developer
Posts: 94
Registered: ‎03-06-2009
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

Hi,

 

I am trying to do the same thing but i am not able to read my mp3 file. can u help me where to put this mp3 file in the project. is it required to add this file to project? I am trying to run it on simulator.

 

Thanks

Highlighted
New Developer
Posts: 4
Registered: ‎08-31-2009
My Device: Not Specified

Re: How to play mp3 or wav sounds for a game?

Put it under the src folder, and then reference it via getClass().getResourceAsStream("/click.mp3").