Search code examples
java-meprefetch

J2ME player object hangs before pre-fetch


I am trying to write code in J2ME for the Nokia SDK (S60 device) and am using Eclipse.

The code tries to play some wav files placed within the "res" directory of the project. The code is as follows:

InputStream in1 = null;
        System.out.println("ABout to play voice:" + i);
        try {
            System.out.println("Getting the resource as stream.");
            in1 = getClass().getResourceAsStream(getsound(i));
            System.out.println("Got the resouce. Moving to get a player");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        try {
            player = Manager.createPlayer(in1, "audio/x-wav");
            System.out.println("Created player.");
            //player.realize();
            //System.out.println("Realized the player.");
            if(player.getState() != player.REALIZED) {
                System.out.println("The player has been realized.");
                player.realize();
            }
            player.prefetch();
            System.out.println("Fetched player. Now starting to play sound.");
            player.start();
            in1.close();
            int i1 = player.getState();
            System.out.println("Player opened. Playing requested sound.");
            //player.deallocate();
            //System.out.println("Deallocated the player.");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

Where the function getSound returns a string that contains the name of the file to be played. It is as follows:

private String getSound(int i) {
   switch(i) {  
     case 1: return "/x1.wav";
     case 2: return "/x2.wav";
   }
}

My problem is this: 1. When I try to add more than 10 sounds, the entire application hangs right before the prefetch() function is called. The entire system slows down considerably for a while. I then have to restart the application.
I have tried to debug this, but have not gotten any solutions so far. It would be great if I could get some help on this.


Solution

  • The problem lies in the emulator being used for the project. In the emulation tab in the Run Configurations window, the following Device must be selected:

    Group: Nokia N97 SDK v1.0
    Device: S60 Emulator

    Changing to the above from Devices listed under the Sun Java Wireless Toolkit solved the problem.