AudioOutputControlMidlet.java
/**
* Copyright (c) 2012 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example;
import javax.microedition.media.control.VideoControl;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import java.io.IOException;
import com.nokia.mid.media.AudioOutputControl;
import com.nokia.mid.media.AudioOutput;
import javax.microedition.amms.GlobalManager;
import javax.microedition.midlet.MIDlet;
public class AudioOutputControlMidlet extends MIDlet implements CommandListener, PlayerListener {
private Form form;
private StringItem audioOutputStr;
// Commands
private Command exitCommand;
private Command clear;//Clear Form
private Command play; // Start playing
private Command pause;// Stop playing
private Command close; // close the player instance
private Command createMIDIPlayer; // Command to create MIDI player
private Command createWavPlayer; // Command to create Wave player
private Command createVideoPlayer; // Command to create video player
// Commands to set audio output preference
private Command setPreferencePublic;
private Command setPreferencePrivate;
private Command setPreferenceAll;
private Command setPreferenceNone;
private Command setPreferenceNoPreference;
// Commands to get preferences
private Command getPreference;
private Command getCurrentPreference;
// Command to get all supported preferences
private Command getAllAvailablePreference;
private Player player;
private int i;
private Item temp;
private boolean videoPlayer = false, playerDeleted = false;
public AudioOutputControl audioOutputControl;
public void startApp() {
form = new Form(null, new Item[]{getHelloStringItem()});
form.addCommand(getExitCommand());
form.addCommand(getCreateMIDICommand());
form.addCommand(getCreateWavCommand());
form.addCommand(getCreateVideoPlayerCommand());
form.addCommand(getPlayCommand());
form.addCommand(getPauseCommand());
form.addCommand(getGetCurrentPreferenceCommand());
form.addCommand(getGetPreferenceCommand());
form.addCommand(getGetAllAvailablePreferenceCommand());
form.addCommand(getSetPreferenceNoPreferenceCommand());
form.addCommand(getSetPreferenceAllCommand());
form.addCommand(getSetPreferenceNoneCommand());
form.addCommand(getSetPreferencePrivateCommand());
form.addCommand(getSetPreferencePublicCommand());
form.addCommand(getCloseCommand());
form.addCommand(getClearCommand());
form.setCommandListener(this);
getDisplay().setCurrent(form);
}
public StringItem getHelloStringItem() {
if (audioOutputStr == null) {
audioOutputStr = new StringItem(" Audio Output Control Example ", "");
}
return audioOutputStr;
}
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
}
if (command == play) {
try {
if (playerDeleted == false) {
player.start();
} else if (playerDeleted == true) {
form.deleteAll();
i = form.append(temp);
player.start();
}
} catch (MediaException me) {
}
}
if (command == pause) {
try {
player.stop();
} catch (MediaException me) {
}
}
if (command == clear) {
player.close();
form.deleteAll();
}
if (command == close) {
player.close();
}
if (command == setPreferencePrivate) {
setAudioOutputPreferences(AudioOutputControl.PRIVATE);
form.append("\n Set To PRIVATE Mode");
}
if (command == setPreferencePublic) {
setAudioOutputPreferences(AudioOutputControl.PUBLIC);
form.append("\n Set To PUBLIC Mode");
}
if (command == setPreferenceAll) {
setAudioOutputPreferences(AudioOutputControl.ALL);
form.append("\n Set To ALL Mode");
}
if (command == setPreferenceNone) {
setAudioOutputPreferences(AudioOutputControl.NONE);
form.append("\n Set To None Mode");
}
if (command == setPreferenceNoPreference) {
setAudioOutputPreferences(AudioOutputControl.DEFAULT);
form.append("\n Set To Default Mode");
}
if (command == getPreference) {
try {
int preference = audioOutputControl.getOutputMode();
form.append("\nMode : " + preference);
} catch (Exception e) {
form.append("\ngetpreference Exception : " + e.toString());
}
}
if (command == getCurrentPreference) {
AudioOutput audioOutputObj = audioOutputControl.getCurrent();
form.append("\nCurrentMode : " + audioOutputObj.getActiveOutputMode());
}
if (command == getAllAvailablePreference) {
try {
int[] ret = audioOutputControl.getAvailableOutputModes(); //Setting up a variable to hold the return value from the method that tells us available output modes
form.append("Available audio output modes: \n"); //Showing the results to the user
for (int i = 0; i < ret.length; i++) //Gets each return value from the array
{
form.append(" ");
displayMode(ret[i]);
}
} catch (Exception e) {
form.append("\nException : " + e.toString());
}
}
if (command == createMIDIPlayer) {
try {
player = Manager.createPlayer(getClass().getResourceAsStream("/audio1.mid"), "audio/midi");
player.realize();
audioOutputControl = (AudioOutputControl) player.getControl("com.nokia.mid.media.AudioOutputControl");
if (audioOutputControl == null) {
audioOutputControl = (AudioOutputControl) GlobalManager.getControl("com.nokia.mid.media.AudioOutputControl");
}
if (audioOutputControl == null) {
form.append("\ncreateLocalPlayer : audioOutputControl not available");
return;
}
player.prefetch();
player.addPlayerListener(this);
} catch (IOException ioe) {
form.append("Getting IOException:" + ioe.toString());
} catch (MediaException me) {
form.append("Getting MediaException:" + me.toString());
} catch (Exception e) {
form.append("Getting OtherException:" + e.toString());
}
}
if (command == createVideoPlayer) {
try {
player = Manager.createPlayer(getClass().getResourceAsStream("/video1.3gp"), "video/3gp");
player.realize();
player.prefetch();
audioOutputControl = (AudioOutputControl) player.getControl("com.nokia.mid.media.AudioOutputControl");
if (audioOutputControl == null) {
audioOutputControl = (AudioOutputControl) GlobalManager.getControl("com.nokia.mid.media.AudioOutputControl");
}
temp = null;
VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
temp = (Item) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);
form.deleteAll();
i = form.append(temp);
videoPlayer = true;
if (audioOutputControl == null) {
form.append("\ncreateVideoPlayer : audioOutputControl not available");
return;
}
player.addPlayerListener(this);
} catch (IOException ioe) {
form.append("Getting IOException:" + ioe.toString());
} catch (MediaException me) {
form.append("Getting MediaException:" + me.toString());
} catch (Exception e) {
form.append("Getting OtherException:" + e.toString());
}
}
if (command == createWavPlayer) {
try {
player = Manager.createPlayer(getClass().getResourceAsStream("/audio2.wav"), "audio/wav");
player.realize();
audioOutputControl = (AudioOutputControl) player.getControl("com.nokia.mid.media.AudioOutputControl");
if (audioOutputControl == null) {
audioOutputControl = (AudioOutputControl) GlobalManager.getControl("com.nokia.mid.media.AudioOutputControl");
}
if (audioOutputControl == null) {
form.append("\ncreateWavPlayer : audioOutputControl not available");
return;
}
player.prefetch();
player.addPlayerListener(this);
} catch (IOException ioe) {
form.append("Getting IOException:" + ioe.toString());
} catch (MediaException me) {
form.append("Getting MediaException:" + me.toString());
} catch (Exception e) {
form.append("Getting OtherException:" + e.toString());
}
}
}
}
private void displayMode(int mode) {
switch (mode) {
case AudioOutputControl.DEFAULT:
form.append("DEFAULT: " + mode + "\n");
break;
case AudioOutputControl.ALL:
form.append("ALL:" + mode + "\n");
break;
case AudioOutputControl.NONE:
form.append("NONE:" + mode + "\n");
break;
case AudioOutputControl.PRIVATE:
form.append("PRIVATE: " + mode + "\n");
break;
case AudioOutputControl.PUBLIC:
form.append("PUBLIC: " + mode + "\n");
break;
default:
form.append("Unexpected return: " + mode + "\n");
}
}
public Display getDisplay() {
return Display.getDisplay(this);
}
public void exitMIDlet() {
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
public Command getExitCommand() {
if (exitCommand == null) {
exitCommand = new Command("Exit", Command.EXIT, 1);
}
return exitCommand;
}
public Command getCreateMIDICommand() {
if (createMIDIPlayer == null) {
createMIDIPlayer = new Command("Create audio player (MIDI)", Command.ITEM, 2);
}
return createMIDIPlayer;
}
public Command getCreateWavCommand() {
if (createWavPlayer == null) {
createWavPlayer = new Command("Create audio player (WAV)", Command.ITEM, 3);
}
return createWavPlayer;
}
public Command getCreateVideoPlayerCommand() {
if (createVideoPlayer == null) {
createVideoPlayer = new Command("Create video player (3GP)", Command.ITEM, 4);
}
return createVideoPlayer;
}
public Command getPlayCommand() {
if (play == null) {
play = new Command("Start playback", Command.ITEM, 5);
}
return play;
}
public Command getPauseCommand() {
if (pause == null) {
pause = new Command("Stop playback", Command.ITEM, 6);
}
return pause;
}
public Command getGetCurrentPreferenceCommand() {
if (getCurrentPreference == null) {
getCurrentPreference = new Command("Get active mode", Command.ITEM, 7);
}
return getCurrentPreference;
}
public Command getGetPreferenceCommand() {
if (getPreference == null) {
getPreference = new Command("Get mode", Command.ITEM, 8);
}
return getPreference;
}
public Command getGetAllAvailablePreferenceCommand() {
if (getAllAvailablePreference == null) {
getAllAvailablePreference = new Command("Get supported modes", Command.ITEM, 9);
}
return getAllAvailablePreference;
}
public Command getSetPreferenceNoPreferenceCommand() {
if (setPreferenceNoPreference == null) {
setPreferenceNoPreference = new Command("Set mode DEFAULT", Command.ITEM, 10);
}
return setPreferenceNoPreference;
}
public Command getSetPreferenceAllCommand() {
if (setPreferenceAll == null) {
setPreferenceAll = new Command("Set mode ALL", Command.ITEM, 11);
}
return setPreferenceAll;
}
public Command getSetPreferenceNoneCommand() {
if (setPreferenceNone == null) {
setPreferenceNone = new Command("Set mode NONE", Command.ITEM, 12);
}
return setPreferenceNone;
}
public Command getSetPreferencePrivateCommand() {
if (setPreferencePrivate == null) {
setPreferencePrivate = new Command("Set mode PRIVATE", Command.ITEM, 13);
}
return setPreferencePrivate;
}
public Command getSetPreferencePublicCommand() {
if (setPreferencePublic == null) {
setPreferencePublic = new Command("Set mode PUBLIC", Command.ITEM, 14);
}
return setPreferencePublic;
}
public Command getCloseCommand() {
if (close == null) {
close = new Command("Close player", Command.ITEM, 15);
}
return close;
}
public Command getClearCommand() {
if (clear == null) {
clear = new Command("Clear Form", Command.ITEM, 16);
}
return clear;
}
public void setAudioOutputPreferences(int mode) {
audioOutputControl.setOutputMode(mode);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
setAudioOutputPreferences(AudioOutputControl.DEFAULT);
}
public void playerUpdate(Player aPlayer, String aEvent, Object aEventData) {
if (aEvent.equals("com.nokia.audiooutputchange.event")) {
int k = ((AudioOutput) aEventData).getActiveOutputMode();
switch (k) {
case 0:
form.append("\nAudioOutputModeChangedTo: DEFAULT");
break;
case 1:
form.append("\nAudioOutputModeChangedTo: ALL");
break;
case 2:
form.append("\nAudioOutputModeChangedTo: NONE");
break;
case 3:
form.append("\nAudioOutputModeChangedTo: PRIVATE");
break;
case 4:
form.append("\nAudioOutputModeChangedTo: PUBLIC");
break;
default:
form.append("Unexpected return:");
}
}
if (aEvent.equals(PlayerListener.END_OF_MEDIA) && videoPlayer == true) {
form.delete(i);
playerDeleted = true;
}
}
}