Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

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);
    }
}
 
Powered by MediaWiki