×
Namespaces

Variants
Actions

How to play music files using JSR 135

From Nokia Developer Wiki
Jump to: navigation, search

This MIDlet demonstrates how to get an audio file from a server, store it into RMS, and play it. It also shows how to play a file from Resources

Article Metadata
Article
Created: senthilkumar05 (19 Dec 2007)
Last edited: hamishwillee (13 Aug 2013)
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);
}
}
This page was last modified on 13 August 2013, at 08:33.
66 page views in the last 30 days.
×