Categories: MMAPI (JSR-135) | Java | Java ME | How To | Code Examples
This page was last modified 01:22, 4 July 2008.
How to play music files using JSR 135
From Forum Nokia Wiki
/** * This MIDlet demonstrates , how to get the audio file from the server * and store it into RMS, and play it, and also how to play file from Resources */ import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.media.*; import javax.microedition.media.control.*; import javax.microedition.rms.*; public class AudioRMS extends MIDlet implements CommandListener, Runnable { private Display mDisplay; private List mMainScreen; public void startApp() { mDisplay = Display.getDisplay(this); if (mMainScreen == null) { mMainScreen = new List("AudioMIDlet", List.IMPLICIT); mMainScreen.append("Via HTTP", null); mMainScreen.append("From resource", null); mMainScreen.addCommand(new Command("Exit", Command.EXIT, 0)); mMainScreen.addCommand(new Command("Play", Command.SCREEN, 0)); mMainScreen.setCommandListener(this); } mDisplay.setCurrent(mMainScreen); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) notifyDestroyed(); else { Form waitForm = new Form("Loading..."); mDisplay.setCurrent(waitForm); Thread t = new Thread(this); t.start(); } } public void run() { String selection = mMainScreen.getString( mMainScreen.getSelectedIndex()); boolean viaHttp = selection.equals("Via HTTP"); if (viaHttp) playFromRMS(); else playFromResource(); } private void playFromRMS() { try { System.out.println("connecting."); String url ="http://localhost/test-wav.wav"; HttpConnection con= (HttpConnection)Connector.open(url); InputStream is = con.openInputStream(); // Storing Audio in the RMS...... ByteArrayOutputStream bstr = new ByteArrayOutputStream(); int ch; while((ch = is.read())!= -1) bstr.write(ch); byte [] byteAudio = bstr.toByteArray(); RecordStore rs = RecordStore.openRecordStore("Audio_Rec",true); int recid; recid = rs.addRecord(byteAudio,0,byteAudio.length); // TO get an Audio from RMS............ InputStream is1 = new ByteArrayInputStream(rs.getRecord(recid)); Player player = Manager.createPlayer(is1,"audio/x-wav"); player.realize(); VolumeControl vc = (VolumeControl)player.getControl("VolumeControl"); if(vc!=null) { vc.setLevel(100); } player.prefetch(); player.start(); } catch (Exception e) { showException(e); return; } mDisplay.setCurrent(mMainScreen); } private void playFromResource() { try { InputStream in = getClass().getResourceAsStream("/test-mp3.mp3"); System.out.println("connected from local"); Player player = Manager.createPlayer(in, "audio/mpeg"); player.start(); } catch (Exception e) { showException(e); return; } mDisplay.setCurrent(mMainScreen); } private void showException(Exception e) { Alert a = new Alert("Exception", e.toString(), null, null); a.setTimeout(Alert.FOREVER); mDisplay.setCurrent(a, mMainScreen); } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multiple channel support with J2ME | paulwatt | Mobile Java Media (Graphics & Sounds) | 2 | 2005-12-08 23:05 |
| Problem of running video clip (in 3gp formate) on S60 3rd edition SDK | AWAIS_CIIT | Mobile Java Media (Graphics & Sounds) | 5 | 2007-07-31 10:18 |
| Play audio from private path | sruthimp | General Symbian C++ | 9 | 2007-08-27 11:38 |
| CDrmPlayerUtility can freeze application | ruisniemi | Symbian Media (Graphics & Sounds) | 6 | 2007-09-06 08:47 |
| Snapshot capture size with MMAPI | Gotcha80 | Mobile Java Media (Graphics & Sounds) | 1 | 2005-04-13 07:24 |
