📄 renderingcontrolservice.java
字号:
package no.auc.one.portableplayer.lmr;
import java.util.Hashtable;
import javax.microedition.media.*;
import javax.microedition.media.control.VolumeControl;
import no.auc.one.portableplayer.communication.upnphosting.*;
import no.auc.one.portableplayer.utils.Utilities;
public final class RenderingControlService extends UPnPHostingServiceBase {
private static org.apache.log4j.Logger LOG =
org.apache.log4j.Logger.getLogger("LMR/RCS");
private PlayerInfo pi = null;
//required Actions
UPnPAction ListPresets = new ListPresetsAction();
UPnPAction SelectPresets = new SelectPresetsAction();
//optional Actions
UPnPAction GetMute = new GetMuteAction();
UPnPAction SetMute = new SetMuteAction();
UPnPAction GetVolume = new GetVolumeAction();
UPnPAction SetVolume = new SetVolumeAction();
UPnPAction GetVolumeDB = new GetVolumeDBAction();
UPnPAction SetVolumeDB = new SetVolumeDBAction();
UPnPAction GetVolumeDBRange = new GetVolumeDBRangeAction();
//internal variables for required State Variables
private String lastChange;
private String presetNameList;
private Hashtable actions;
public RenderingControlService(PlayerInfo pi) {
this.pi = pi;
actions = new Hashtable(9);
actions.put(ListPresets.getName(), ListPresets);
actions.put(SelectPresets.getName(), SelectPresets);
actions.put(GetMute.getName(), GetMute);
actions.put(SetMute.getName(), SetMute);
actions.put(GetVolume.getName(), GetVolume);
actions.put(SetVolume.getName(), SetVolume);
actions.put(GetVolumeDB.getName(), GetVolumeDB);
actions.put(SetVolumeDB.getName(), SetVolumeDB);
actions.put(GetVolumeDBRange.getName(), GetVolumeDBRange);
}
public String serviceType() {
return "urn:schemas-upnp-org:service:RenderingControl:1";
}
public String serviceId() {
return "urn:upnp-org:serviceId:RenderingControl";
}
public UPnPStateVariable[] stateVariableList() {
return new UPnPStateVariable[]{
new UPnPStateVariable(
"LastChange",
UPnPStateVariable.STATEVARIABLE_DATATYPE_STRING),
new UPnPStateVariable(
"PresetNameList",
UPnPStateVariable.STATEVARIABLE_DATATYPE_STRING,
false),
new UPnPStateVariable(
"Mute",
UPnPStateVariable.STATEVARIABLE_DATATYPE_BOOLEAN,
false),
new UPnPStateVariable(
"Volume",
UPnPStateVariable.STATEVARIABLE_DATATYPE_UI2,
false,
new UPnPValueRange(
new Integer(0),
new Integer(100),
new Integer(1))),
new UPnPStateVariable(
"VolumeDB",
UPnPStateVariable.STATEVARIABLE_DATATYPE_I2,
false,
new UPnPValueRange(
new Integer(-100),
new Integer(100))),
new UPnPStateVariable(
"A_ARG_TYPE_Channel",
UPnPStateVariable.STATEVARIABLE_DATATYPE_STRING,
false,
new String[]{"Master"}),
new UPnPStateVariable(
"A_ARG_TYPE_InstanceID",
UPnPStateVariable.STATEVARIABLE_DATATYPE_UI4,
false),
new UPnPStateVariable(
"A_ARG_TYPE_PresetName",
UPnPStateVariable.STATEVARIABLE_DATATYPE_STRING,
false,
new String[]{"FactoryDefaults"})};
}
public Hashtable actionTable() {
return actions;
}
private class ListPresetsAction extends UPnPAction {
public ListPresetsAction() {
super(
"ListPresets",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"CurrentPresetNameList",
UPnPActionArgument.ARGUMENT_DIRECTION_OUT,
"PresetNameList")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
args[1].setValue(presetNameList);
}
}
private class SelectPresetsAction extends UPnPAction {
public SelectPresetsAction() {
super(
"SelectPresets",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"PresetName",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_PresetName")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedPresetName = (String)args[1].getValue();
if (presetNameList.indexOf(requestedPresetName) == -1) {
// throw exception... preset does not exist
// Error code 701 - Invalid Name - The specified name is not a valid preset name.
} else {
// XXX Change volume etc. according to the specified preset.
}
}
}
private class GetMuteAction extends UPnPAction {
public GetMuteAction() {
super(
"GetMute",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"Channel",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_Channel"),
new UPnPActionArgument(
"CurrentMute",
UPnPActionArgument.ARGUMENT_DIRECTION_OUT,
"Mute")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedChannel = (String)args[1].getValue();
if (requestedChannel.equals("Master")) {
VolumeControl vc = (VolumeControl)pi.getPlayer().getControl("VolumeControl");
if (vc != null) {
args[2].setValue(new Boolean(vc.isMuted()));
} else {
LOG.warn("Could not get volume control for player...");
args[2].setValue(new Boolean(false));
}
} else {
// throw exception - 703 - Invalid Channel - The specified channel is invalid
}
}
}
private class SetMuteAction extends UPnPAction {
public SetMuteAction() {
super(
"SetMute",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"Channel",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_Channel"),
new UPnPActionArgument(
"DesiredMute",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"Mute")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedChannel = (String)args[1].getValue();
if (!requestedChannel.equals("Master")) {
//
// throw exception - 703 - Invalid Channel - The specified channel is invalid
//
return;
}
Player p = pi.getPlayer();
if (p == null) {
LOG.warn("Player == null");
return; // or throw exception
}
VolumeControl vc = (VolumeControl)pi.getPlayer().getControl("VolumeControl");
if (vc == null) {
LOG.warn("VC == null");
return;
}
boolean requestedMute = Utilities.parseBoolean(
(String)args[2].getValue());
vc.setMute(requestedMute);
}
}
private class GetVolumeAction extends UPnPAction {
public GetVolumeAction() {
super(
"GetVolume",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"Channel",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_Channel"),
new UPnPActionArgument(
"CurrentVolume",
UPnPActionArgument.ARGUMENT_DIRECTION_OUT,
"Volume")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedChannel = (String)args[1].getValue();
if (requestedChannel.equals("Master")) {
VolumeControl vc = (VolumeControl)pi.getPlayer().getControl("VolumeControl");
if (vc != null) {
args[2].setValue(new Integer(vc.getLevel()));
} else {
LOG.warn("Could not get volume control for player...");
args[2].setValue(new Integer(0));
}
} else {
// throw exception - 703 - Invalid Channel - The specified channel is invalid
}
}
}
private class SetVolumeAction extends UPnPAction {
public SetVolumeAction() {
super(
"SetVolume",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"Channel",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_Channel"),
new UPnPActionArgument(
"DesiredVolume",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"Volume")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedChannel = (String)args[1].getValue();
if (requestedChannel.equals("Master")) {
VolumeControl vc = (VolumeControl)pi.getPlayer().getControl("VolumeControl");
if (vc != null) {
int desiredVolume = Integer.parseInt((String)args[2].getValue());
vc.setLevel(desiredVolume);
} else {
LOG.warn("Could not get volume control for player...");
args[2].setValue(new Integer(0));
}
} else {
// throw exception - 703 - Invalid Channel - The specified channel is invalid
}
}
}
private class GetVolumeDBAction extends UPnPAction {
public GetVolumeDBAction() {
super(
"GetVolumeDB",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"Channel",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_Channel"),
new UPnPActionArgument(
"CurrentVolume",
UPnPActionArgument.ARGUMENT_DIRECTION_OUT,
"VolumeDB")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedChannel = (String)args[1].getValue();
if (requestedChannel.equals("Master")) {
VolumeControl vc = (VolumeControl)pi.getPlayer().getControl("VolumeControl");
if (vc != null) {
int currentLevel = vc.getLevel();
// XXX Convert currentLevel to dB
args[2].setValue(new Integer(currentLevel));
} else {
LOG.warn("Could not get volume control for player...");
args[2].setValue(new Integer(0));
}
} else {
// throw exception - 703 - Invalid Channel - The specified channel is invalid
}
}
}
private class SetVolumeDBAction extends UPnPAction {
public SetVolumeDBAction() {
super(
"SetVolumeDB",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"Channel",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_Channel"),
new UPnPActionArgument(
"DesiredVolume",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"VolumeDB")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedChannel = (String)args[1].getValue();
if (requestedChannel.equals("Master")) {
VolumeControl vc = (VolumeControl)pi.getPlayer().getControl("VolumeControl");
if (vc != null) {
int desiredVolume = Integer.parseInt((String)args[2].getValue());
// XXX Convert desiredVolume to 'level'
vc.setLevel(desiredVolume);
} else {
LOG.warn("Could not get volume control for player...");
}
} else {
// throw exception - 703 - Invalid Channel - The specified channel is invalid
}
}
}
private class GetVolumeDBRangeAction extends UPnPAction {
public GetVolumeDBRangeAction() {
super(
"GetVolumeDBRange",
new UPnPActionArgument[]{
new UPnPActionArgument(
"InstanceID",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_InstanceID"),
new UPnPActionArgument(
"Channel",
UPnPActionArgument.ARGUMENT_DIRECTION_IN,
"A_ARG_TYPE_Channel"),
new UPnPActionArgument(
"MinValue",
UPnPActionArgument.ARGUMENT_DIRECTION_OUT,
"VolumeDB"),
new UPnPActionArgument(
"MinValue",
UPnPActionArgument.ARGUMENT_DIRECTION_OUT,
"VolumeDB")});
}
public void invoke(UPnPActionArgument[] args) {
int instanceId = Integer.parseInt((String)args[0].getValue());
String requestedChannel = (String)args[1].getValue();
if (requestedChannel.equals("Master")) {
args[2].setValue(new Integer(-100));
args[3].setValue(new Integer(100));
} else {
// throw exception - 703 - Invalid Channel - The specified channel is invalid
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -