Here is the code sample for reading a audio file from mobile file system and plays. Here i have used JSR 75 for FileConnection and player class for playing the audio.

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.media.*;
public class MyFileConnection extends MIDlet implements CommandListener {
    private Display display;
    private Command Exit;
    private Form f;
    public MyFileConnection() {
        // TODO Auto-generated constructor stub
        f = new Form("Fileconnection Sample");
        Exit = new Command("Exit", Command.EXIT, 0);
        f.addCommand(Exit);
        f.setCommandListener(this);
    }
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub
    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
        display = Display.getDisplay(this);
        // f.append(System.getProperty("microedition.io.file.FileConnection.version"));
        String s = System
                .getProperty("microedition.io.file.FileConnection.version");
        String roots="";
        if (s != null) {
            // Template to list all the available File systems.
            Enumeration en = FileSystemRegistry.listRoots();
            StringBuffer sf = new StringBuffer();
            while (en.hasMoreElements()) {
                 roots = (String) en.nextElement();
                sf.append("file directory " + roots);
                sf.append("\n");
            }
            f.append(sf.toString());
            display.setCurrent(f);
        } else {
            s = "No file system supported";
            f.append(s);
            display.setCurrent(f);
        }
        // //////////////////////////////////////////////////
        FileConnection fcon = null;
        String content = null;
        try {
            fcon = (FileConnection)Connector.open("file:///C:/Attachments/test.mp3",Connector.READ_WRITE);
            if (!fcon.exists())
                throw new Exception("File does not exist");
            // fcon.create();
             //OutputStream os=fcon.openOutputStream();
             //String s="Hi Ranjan";
             //byte[] b=s.getBytes();
             //os.write(b);
             //os.close();
             //b=null;
            InputStream is = fcon.openInputStream();
            Player player = Manager.createPlayer(is, "audio/mpeg");
            if (player != null) {
            player.start();
            }
        } catch (Exception e) {
            Alert alert = new Alert("Error", "this is"+e.toString(), null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        //f.append("Content is" + content);
        //display.setCurrent(f);
    }
    public void commandAction(Command c, Displayable d) {
        if (c == Exit) {
            try {
                destroyApp(false);
                notifyDestroyed();
            } catch (Exception e) {
            }
        }
    }
}

 

This article is about: Code Sample, J2ME,

Stacktips

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages. Follow us on Twitter

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • javatechig

    file:///C:/Attachments/test.mp3 is basically the file path for the test.mp3. You can always give your own path/ location of your media file. it can be either C i.e device memory or external memory card path.

    visit the below link for the details on the device file system.

    http://www.codinguru.com/2011/01/j2me-file-browser-jsr-75.html

  • Anonymous

    I want to know about this link file:///C:/Attachments/test.mp3 ????