Java by API/javax.microedition.media/PlayerListener
Содержание
implements PlayerListener
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.midlet.MIDlet;
public class EchoEventsMIDlet extends MIDlet implements PlayerListener {
private StringItem stringItem = new StringItem("", null);
public void startApp() {
try {
Form form = new Form("Player State");
form.append(stringItem);
Display.getDisplay(this).setCurrent(form);
Player player = Manager.createPlayer(getClass().getResourceAsStream("/b.wav"), "audio/x-wav");
player.addPlayerListener(this);
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void playerUpdate(Player player, String event, Object eventData) {
stringItem.setText(event);
System.err.println(event);
}
}
PlayerListener.CLOSED
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;
public class EventHandler implements PlayerListener {
private StringItem item;
public EventHandler(StringItem item) {
this.item = item;
}
public void playerUpdate(Player player, String event, Object eventData) {
if (event == (PlayerListener.VOLUME_CHANGED)) {
VolumeControl vc = (VolumeControl) eventData;
updateDisplay("Volume Changed to: " + vc.getLevel());
if (vc.getLevel() > 60) {
updateDisplay("Volume higher than 60 is too loud");
vc.setLevel(60);
}
} else if (event == (PlayerListener.STOPPED)) {
updateDisplay("Player paused at: " + (Long) eventData);
} else if (event == (PlayerListener.STARTED)) {
updateDisplay("Player started at: " + (Long) eventData);
} else if (event == (PlayerListener.END_OF_MEDIA)) {
updateDisplay("Player reached end of loop.");
} else if (event == (PlayerListener.CLOSED)) {
updateDisplay("Player closed.");
} else if (event == (PlayerListener.ERROR)) {
updateDisplay("Error Message: " + (String) eventData);
}
}
public void updateDisplay(String text) {
item.setText(text);
System.err.println(text);
}
}
PlayerListener.END_OF_MEDIA
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;
public class EventHandler implements PlayerListener {
private StringItem item;
public EventHandler(StringItem item) {
this.item = item;
}
public void playerUpdate(Player player, String event, Object eventData) {
if (event == (PlayerListener.VOLUME_CHANGED)) {
VolumeControl vc = (VolumeControl) eventData;
updateDisplay("Volume Changed to: " + vc.getLevel());
if (vc.getLevel() > 60) {
updateDisplay("Volume higher than 60 is too loud");
vc.setLevel(60);
}
} else if (event == (PlayerListener.STOPPED)) {
updateDisplay("Player paused at: " + (Long) eventData);
} else if (event == (PlayerListener.STARTED)) {
updateDisplay("Player started at: " + (Long) eventData);
} else if (event == (PlayerListener.END_OF_MEDIA)) {
updateDisplay("Player reached end of loop.");
} else if (event == (PlayerListener.CLOSED)) {
updateDisplay("Player closed.");
} else if (event == (PlayerListener.ERROR)) {
updateDisplay("Error Message: " + (String) eventData);
}
}
public void updateDisplay(String text) {
item.setText(text);
System.err.println(text);
}
}
PlayerListener.STARTED
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;
public class EventHandler implements PlayerListener {
private StringItem item;
public EventHandler(StringItem item) {
this.item = item;
}
public void playerUpdate(Player player, String event, Object eventData) {
if (event == (PlayerListener.VOLUME_CHANGED)) {
VolumeControl vc = (VolumeControl) eventData;
updateDisplay("Volume Changed to: " + vc.getLevel());
if (vc.getLevel() > 60) {
updateDisplay("Volume higher than 60 is too loud");
vc.setLevel(60);
}
} else if (event == (PlayerListener.STOPPED)) {
updateDisplay("Player paused at: " + (Long) eventData);
} else if (event == (PlayerListener.STARTED)) {
updateDisplay("Player started at: " + (Long) eventData);
} else if (event == (PlayerListener.END_OF_MEDIA)) {
updateDisplay("Player reached end of loop.");
} else if (event == (PlayerListener.CLOSED)) {
updateDisplay("Player closed.");
} else if (event == (PlayerListener.ERROR)) {
updateDisplay("Error Message: " + (String) eventData);
}
}
public void updateDisplay(String text) {
item.setText(text);
System.err.println(text);
}
}
PlayerListener.STOPPED
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;
public class EventHandler implements PlayerListener {
private StringItem item;
public EventHandler(StringItem item) {
this.item = item;
}
public void playerUpdate(Player player, String event, Object eventData) {
if (event == (PlayerListener.VOLUME_CHANGED)) {
VolumeControl vc = (VolumeControl) eventData;
updateDisplay("Volume Changed to: " + vc.getLevel());
if (vc.getLevel() > 60) {
updateDisplay("Volume higher than 60 is too loud");
vc.setLevel(60);
}
} else if (event == (PlayerListener.STOPPED)) {
updateDisplay("Player paused at: " + (Long) eventData);
} else if (event == (PlayerListener.STARTED)) {
updateDisplay("Player started at: " + (Long) eventData);
} else if (event == (PlayerListener.END_OF_MEDIA)) {
updateDisplay("Player reached end of loop.");
} else if (event == (PlayerListener.CLOSED)) {
updateDisplay("Player closed.");
} else if (event == (PlayerListener.ERROR)) {
updateDisplay("Error Message: " + (String) eventData);
}
}
public void updateDisplay(String text) {
item.setText(text);
System.err.println(text);
}
}