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) { } } } }
