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
Highlighted
New Developer
Posts: 1
Registered: ‎07-24-2009
My Device: Not Specified

Buffering an mp3 clip

Hi,

 

I am creating an application where an mp3 sound clips need to play when a button is pressed. The problem I am having is buffering the mp3 so that the clip plays as soon as your press the button. At the moment it is taking a second or 2 to start playing the 5KB mp3. How can I decrease the time it takes to load the file and start playing it? The button may be pressed with very short intervals in between so I need the mp3 buffered again to play asap. The code I am using just now is as follows (very basic). Any help would be appreciated.

 

import java.io.InputStream;

import javax.microedition.media.control.*;

import javax.microedition.media.*;

 

public class PlayerThread extends Thread{

 

Player
player;VolumeControl vc;

 

public void run()

{

 

try

{

InputStream is = getClass().getResourceAsStream(
"/Sound203.mp3"); player = Manager.createPlayer(is,"audio/mpeg");

 

player.realize();

vc = (VolumeControl) player.getControl("VolumeControl"); if(vc != null)

{

vc.setLevel(100);

}

player.prefetch();

player.start();

}

catch(Exception e)

{}

}

 

}

Developer
Posts: 53
Registered: ‎01-08-2009
My Device: Not Specified

Re: Buffering an mp3 clip

You could probably create a new Player object for each sound clip and have each one initialized (all of your code up to and including prefetch).  Then they will be ready to play as soon as you call start().  Keep in mind that on CDMA devices, you can only have one audio source (Player) playing at a time so if you call start() on a second player before the first player is done, the first one will be automatically stopped by the system.

 

If that doesn't work (I haven't tried it so I can't say if it will or not), you might also have luck loading the MP3 files into byte arrays and keeping them in memory (in case the getResourceAsStream is slowing things down).  Then just use a ByteArrayInputStream with each cached byte[] as your source.

 

Marc

New Developer
Posts: 10
Registered: ‎09-18-2009
My Device: Not Specified

Re: Buffering an mp3 clip

Hey,

 

Just wondering... Were you able to make this work? I'm building something similar...

 

I have several mp3's that, together, make a frase. I need to play them one after the other; and right now there's the delay you talked about between them. I tried creating 4 different players, prefetching them and all that; but the delay stays the same...

 

I ask for the player's state and, if it is STARTED, wait until it has finished to make the next player.start()... Don't know if that's what's causing the delay...

 

Any thoughts?


Thanks

Developer
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Buffering an mp3 clip

this might sound stupid but why can't initialise and prefetch the player before the user clicks start. That way when the button is clicked, make the player start. Player.start(); ? So the delay won't be there.