The AudioHandler
MIDlet is an additional component
to the TextHandler
. It plays audio/x-wav
content
from invoked connections. The AudioHandler
MIDlet itself
does not invoke content, it can only handle incoming requests from the content
invoker, which is this example the TextHandler
.
AudioHandler
MIDlet starts automatically when any audio/x-wav
content
comes as invocation. The RequestListener
method is used
to notify the MIDlet of incoming invocation. It starts a new thread to handle
it.
Create the AudioHandler
class
file.
Import the
required classes and assign this class to the com.nokia.midp.example.jsr211.texthandler
package.
package com.nokia.midp.example.jsr211.audiohandler; import java.io.IOException; import java.io.InputStream; import javax.microedition.content.ContentHandlerServer; import javax.microedition.content.Invocation; import javax.microedition.content.Registry; import javax.microedition.content.RequestListener; import javax.microedition.io.HttpConnection; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.ImageItem; import javax.microedition.lcdui.Item; import javax.microedition.lcdui.Ticker; import javax.microedition.media.Manager; import javax.microedition.media.Player; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException;
Create the main class and the objects to be used in this application.
public class AudioHandler extends MIDlet implements CommandListener, RequestListener { /** Audio Player Form Title*/ private static final String FORM_TITLE = "Audio Player"; /** Audio Player ticker title*/ private static final String TICKER_TITLE = "Playing Audio File"; /** Message constant*/ private static final String MSG_CAN_NOT_READ_AUDIO = "Can not read Audio"; /** Audio content type constant*/ private static final String CONTENT_TYPE_AUDIO = "audio/x-wav"; /** Current class name*/ private static final String CLASS_NAME = "com.nokia.midp.example.jsr211.audiohandler.AudioHandler"; /** Display instance*/ private Display display = null; /** Form to display audio UI*/ private Form audioForm = null; /** Stop audio and return to previous screen */ private Command stopCommand = new Command("Back",Command.OK,1); /** Instance of the player*/ private Player player; /** Current invocation, null if no Invocation. */ private Invocation invocation; /** ContentHandlerServer from which to get requests. */ private ContentHandlerServer handler; /** Access to Registry functions and responses. */ private Registry registry; /** Audio Input Stream stores audio data comes from the server. */ private InputStream audioStream; /** Stores the type of the Invocation content. */ private String reveivedType;
Create a constructor
to initialize the Display
instance.
public AudioHandler() { display = Display.getDisplay(this); try { registry = Registry.getRegistry(AudioHandler.CLASS_NAME); handler = registry.register(AudioHandler.CLASS_NAME); if (handler != null) { handler.setListener(this); } } catch (Exception che) { che.printStackTrace(); } }
Add a new Form
for
the audio player and add some Command
s for its controls.
private void displayAudioPlayForm() { // Remove previous items and command audioForm = new Form(AudioHandler.FORM_TITLE); audioForm.removeCommand(exitCommand); String name = invocation.getURL(); name = name.substring(name.lastIndexOf('/')+1); audioForm.addCommand(stopCommand); audioForm.setCommandListener(this); audioForm.setTicker(new Ticker(AudioHandler.TICKER_TITLE)); try{ ImageItem imageItem = new ImageItem(null, null, Item.LAYOUT_CENTER, "-no image-"); imageItem.setImage(Image.createImage("/com/nokia/midp/example/jsr211/res/audio_player.png")); audioForm.append(imageItem); }catch(Exception e){ } display.setCurrent(audioForm); }
Add the default
MIDlet methods and functionality for the Back
Command
.
protected void startApp() throws MIDletStateChangeException { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } public void commandAction(Command command, Displayable disp){ if (command.getLabel().equals("Back") || command == stopCommand){ try { doFinish(Invocation.OK); destroyApp(true); } catch (MIDletStateChangeException e) { e.printStackTrace(); } } }
Create a method
for handling the incoming invocation request. In this example, the following
code receives the requested information in invoc
instance
and calls method handleAudio(invoc)
to process it.
public void invocationRequestNotify(ContentHandlerServer server) { if (invocation != null) { server.finish(invocation, Invocation.OK); } invocation = server.getRequest(false); if (invocation != null) { handleAudio(invocation); } }
Establish a HTTP connection with the web server. Check if the content
is available and if it can be found, get the required content stated in InputStream
.
private void handleAudio(Invocation _invoc) { HttpConnection conn = null; try { conn = (HttpConnection)_invoc.open(false); // Check the status and read the content int status = conn.getResponseCode(); if (status != HttpConnection.HTTP_OK) { Alert alert = new Alert(AudioHandler.MSG_CAN_NOT_READ_AUDIO, "Audio not available at " + _invoc.getURL(), null, AlertType.ERROR); display.setCurrent(alert); doFinish(Invocation.CANCELLED); return; } reveivedType = conn.getType(); /** * If invocation content type does not match with the app content type then * display main screen without handling the content. */ if (!reveivedType.equals(AudioHandler.CONTENT_TYPE_AUDIO)){ }
Open the audio/x-wav
stream
and run it in the AudioHandler
in its own Thread
.
// Get the image from the connection audioStream = conn.openInputStream(); new Thread(new Runnable() { public void run() { try{ displayAudioPlayForm(); player = Manager.createPlayer(audioStream, AudioHandler.CONTENT_TYPE_AUDIO); player.start(); }catch(Exception e){ Alert alert = new Alert(AudioHandler.MSG_CAN_NOT_READ_AUDIO, "Unable to play Audio", null, AlertType.ERROR); display.setCurrent(alert); doFinish(Invocation.CANCELLED); } } }).start(); } catch (Exception e) { Alert alert = new Alert(AudioHandler.MSG_CAN_NOT_READ_AUDIO, "Audio not available", null, AlertType.ERROR); display.setCurrent(alert); doFinish(Invocation.CANCELLED); } finally { try { if (conn != null) { conn.close(); } } catch (IOException ioe) { } } }
Finalize the invocation process.
void doFinish(int status) { if (invocation != null) { boolean mustExit = handler.finish(invocation, status); invocation = null; if (mustExit) { try { destroyApp(true); } catch (MIDletStateChangeException e) { e.printStackTrace(); } } else { // Application does not need to exit } } } }