📄 .#avtransportservice.java.1.3
字号:
package no.auc.one.portableplayer.communication.mediarenderer;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import no.auc.one.portableplayer.communication.*;
import no.auc.one.portableplayer.communication.soap.*;
import no.auc.one.portableplayer.utils.*;
import no.auc.one.portableplayer.librarymanager.*;
/**
* For controlling a AV Transport Services, used by Media Renderers.
*/
public class AVTransportService extends UPnPService implements Cloneable {
/**
* Construct instance based on a UPnPService.
*/
public AVTransportService(
UPnPService service)
{
super(
service.getServiceType(),
service.getServiceID(),
service.getScpdURL(),
service.getEventSubURL(),
service.getControlURL());
}
/**
* Creates a clone of this class instance.
*
* @return Clone of this class instance.
*/
public Object clone() {
return new AVTransportService((UPnPService)this);
}
/**
* This method requests the AV Transport service to set the URI of the media
* to be played at the Media Renderer.
*
* @param ti Track which shall be set to play.
* @return Returns the SOAP response
* @throws IOException
*/
public String setAVTransportURI(TrackInformation ti) throws IOException {
SoapInvoke si =
new SoapInvoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"",
getControlURL());
StringBuffer metadata = new StringBuffer();
metadata.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
metadata.append("<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\"");
metadata.append("xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" ");
metadata.append("xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\">");
metadata.append("<item id=\"0\" parentID=\"0\" restricted=\"1\">");
metadata.append("<dc:creator>" + ti.getArtist() + "</dc:creator>");
metadata.append("<dc:title>" + ti.getName() + "</dc:title>");
metadata.append("<upnp:artist>" + ti.getArtist() + "</upnp:artist>");
metadata.append("<upnp:album>" + ti.getAlbum() + "</upnp:album>");
metadata.append("<upnp:genre>" + ti.getGenre() + "</upnp:genre>");
metadata.append(
"<res size=\"" + ti.getResourceSize() + "\" duration=\"" +
ti.getDuration() + "\" protocolInfo=\"http-get:*:audio/mpeg:*\">");
metadata.append(ti.getResourceUri() + "</res>");
metadata.append("<upnp:class>object.item.audioItem.musicTrack</upnp:class>"); // XXX
metadata.append("</item>");
metadata.append("</DIDL-Lite>");
StringBuffer buffer = new StringBuffer();
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
buffer.append(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
buffer.append("<s:Body>\r\n");
buffer.append(
"<u:SetAVTransportURI xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">\r\n");
buffer.append("<InstanceID>0</InstanceID>\r\n");
buffer.append("<CurrentURI>" + ti.getResourceUri() + "</CurrentURI>\r\n");
buffer.append("<CurrentURIMetaData>");
buffer.append(StringEscapeUtils.escapeXml(metadata.toString()));
buffer.append("</CurrentURIMetaData>\r\n");
buffer.append("</u:SetAVTransportURI>\r\n");
buffer.append("</s:Body>\r\n");
buffer.append("</s:Envelope>\r\n");
si.setBody(buffer.toString());
String resp = si.invoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI\"");
System.out.println("Response from SetAVTransportURI invoke: \r\n" + resp);
//XXX Should convert the response from the operation to an exception, if the response
// is indeed exceptional. And change the method to be 'void' instead.
return resp;
}
/**
* Request the Media Renderer to transition to Play mode, which will render the
* current URI of the device.
*
* @return SOAP response of the request
* @throws IOException
*/
public String play() throws IOException
{
SoapInvoke si =
new SoapInvoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Play\"",
getControlURL());
StringBuffer buffer = new StringBuffer();
//XXX buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\">\r\n");
buffer.append(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
buffer.append("<s:Body>\r\n");
buffer.append("<u:Play xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">\r\n");
buffer.append("<InstanceID>0</InstanceID>\r\n");
buffer.append("<Speed>1</Speed>\r\n");
buffer.append("</u:Play>\r\n");
buffer.append("</s:Body>\r\n");
buffer.append("</s:Envelope>\r\n");
si.setBody(buffer.toString());
String resp = si.invoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Play\"");
System.out.println("Response from Play invoke: \r\n" + resp);
return resp;
}
/**
* Request the Media Renderer to pause the current media.
*
* @return SOAP response of the request
* @throws IOException
*/
public String pause() throws IOException
{
SoapInvoke si =
new SoapInvoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Pause\"",
getControlURL());
StringBuffer buffer = new StringBuffer();
//XXX buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\">\r\n");
buffer.append(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
buffer.append("<s:Body>\r\n");
buffer.append("<u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">\r\n");
buffer.append("<InstanceID>0</InstanceID>\r\n");
buffer.append("</u:Pause>\r\n");
buffer.append("</s:Body>\r\n");
buffer.append("</s:Envelope>\r\n");
si.setBody(buffer.toString());
String resp = si.invoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Pause\"");
System.out.println("Response from Pause invoke: \r\n" + resp);
return resp;
}
/**
* Request the Media Renderer to stop playing the current media.
*
* @return SOAP response of the request
* @throws IOException
*/
public String stop() throws IOException
{
SoapInvoke si =
new SoapInvoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Stop\"",
getControlURL());
StringBuffer buffer = new StringBuffer();
//XXX buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\">\r\n");
buffer.append(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
buffer.append("<s:Body>\r\n");
buffer.append("<u:Stop xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">\r\n");
buffer.append("<InstanceID>0</InstanceID>\r\n");
buffer.append("</u:Stop>\r\n");
buffer.append("</s:Body>\r\n");
buffer.append("</s:Envelope>\r\n");
si.setBody(buffer.toString());
String resp = si.invoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Stop\"");
System.out.println("Response from Pause invoke: \r\n" + resp);
return resp;
}
/**
* Request the Media Renderer to play the next track.
*
* @return SOAP response of the request
* @throws IOException
*/
public String next() throws IOException
{
SoapInvoke si =
new SoapInvoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Next\"",
getControlURL());
StringBuffer buffer = new StringBuffer();
//XXX buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\">\r\n");
buffer.append(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
buffer.append("<s:Body>\r\n");
buffer.append("<u:Next xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">\r\n");
buffer.append("<InstanceID>0</InstanceID>\r\n");
buffer.append("</u:Next>\r\n");
buffer.append("</s:Body>\r\n");
buffer.append("</s:Envelope>\r\n");
si.setBody(buffer.toString());
String resp = si.invoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Next\"");
System.out.println("Response from Pause invoke: \r\n" + resp);
return resp;
}
/**
* Request the Media Renderer to stop play the previous track.
*
* @return SOAP response of the request
* @throws IOException
*/
public String previous() throws IOException
{
SoapInvoke si =
new SoapInvoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Previous\"",
getControlURL());
StringBuffer buffer = new StringBuffer();
//XXX buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\">\r\n");
buffer.append(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
buffer.append("<s:Body>\r\n");
buffer.append("<u:Previous xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">\r\n");
buffer.append("<InstanceID>0</InstanceID>\r\n");
buffer.append("</u:Previous>\r\n");
buffer.append("</s:Body>\r\n");
buffer.append("</s:Envelope>\r\n");
si.setBody(buffer.toString());
String resp = si.invoke(
"\"urn:schemas-upnp-org:service:AVTransport:1#Prevoius\"");
System.out.println("Response from Pause invoke: \r\n" + resp);
return resp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -