Search code examples
javajava-meaudio-player

How to play .mp3 songs from files in memory card in J2ME


I am trying to create a Music Player App in J2ME. I want to know how can we play the mp3 files stored in memory card and phone memory. below is the code i am trying to execute(in else part). It complies without any error but doesn't play any sound.

playButton.addClickListener(new ClickListener() {
     public void onClick(Component arg0) {
        try{
            if(player.getState() == player.STARTED){
               player.stop();
               player_GUI.playButton.setText("play");
            }
            else{
            player = Manager.createPlayer("file:///E/song1.mp3");
        player_GUI.playButton.setText("pause");
                player.realize();
        player.prefetch();
        player.start();
            }
        }
        catch(Exception e){
           CatchError.showError("click play ", e.getMessage());
        }
 }
        });

Solution

  • You can not access Memorycard directly as a string parameter.for any type of memory access you need to make a FileConnection.i give the code below.

    Player p ;
      FileConnection connection = (FileConnection) Connector.open("file:///M:/sampleVideo/file1.mpg");
            // If you run on the simulator then it is memorycard and if it device then it is specific to that device
           // FileConnection connection = (FileConnection) Connector.open("file:///memorycard/sampleVideo/6.mp4");
                                    InputStream inStream = null;
                                    inStream = connection.openInputStream();
                                    try {
                                        p = Manager.createPlayer(inStream, "video/mpg");
                                        p.addPlayerListener(this);
                                       p.realize();
    

    first you have to find out memory card folder name of that device then only you hard code this as E:.you can find this using Sun's wireless toolkit example PDAPDemo install it on device and find the correct folder name.then you can able to play media file from the memory card.