Take the 2-minute tour ×

i am unable to play an mp3 file one after the other, when 1 file finishes playing the other file needs to start. i am only able to start playing 1 file, when i put code to start the next file it just does nothing. for some reasons i cannot use thread.sleep() for the duration of file. Is there any other way to do this? Thank you! :)

//this is the basic code..
playq(String fname){
pl =  Manager.createPlayer(stream,"audio/mpeg");



// what should i use here?? pls i don't want to use thread.sleep..
void playagain(String fname){
        try {


} catch (Exception ex) {}
 //change file name and stream and..
share|improve this question

1 Answer 1

Your code should NOT attempt to play the code from a catch block - for one thing, it's only going to be called if an exception was generated (You also generally shouldn't be blanket-catching Exception - use something more specific).

Are you sure you can't use Thread.sleep()? Granted you're not really going to want to anyways (for instance, if the user can pause the clip...).

Instead, look into using the PlayerListener interface, and listen for END_OF_MEDIA events.

A really basic (as in, this isn't tested, and needs more work besides) example:

public class PlayerRunner implements PlayerListener {

    private final String[] songFiles;
    private int songIndex = 0;

    public PlayerRunner(String[] songs) {
        this.songFiles = songs;

    public void start() {
        playerUpdate(null, null, null);

    // This method is required by the PlayerListener interface
    public void playerUpdate(Player player, String event, Object eventData) {
        // The first time through all parameters will be blank/null...
        boolean nextSong = (event == null);
        if (event == PlayerListener.END_OF_MEDIA) {
            nextSong = index < songIndex.length;

        if (nextSong) {
            String fileName = songFiles[index++];
            if (fileName != null) {
                Player pl = Manager.createPlayer(fileName, "audio/mpeg");

Please note that I'm not doing this completely correctly - I'm not doing exception handling, for example. Also, without knowing more about your situation, I don't know what other things to worry about. This should just be a simple answer to get you started looking at where you should be going with this.

(Also, I've never worked with the media player of JME, so I don't know any caveats about the GC there, etc).

share|improve this answer
How did you use List<String> in Java ME? –  bharath Sep 20 '11 at 5:52
winces I said I'd never messed with Java ME, and this shows. Changing to use an arraylist. –  Clockwork-Muse Sep 20 '11 at 15:29

Your Answer


