研究DLNA已经3周了,总算搞通DMC了。走了不少弯路,网上查了不少资料,但有用的不多
希望这个对你们有帮助
package com.example.browser;
import org.fourthline.cling.controlpoint.ActionCallback;
import org.fourthline.cling.controlpoint.ControlPoint;
import org.fourthline.cling.model.action.ActionInvocation;
import org.fourthline.cling.model.message.UpnpResponse;
import org.fourthline.cling.model.meta.Action;
import org.fourthline.cling.model.meta.Device;
import org.fourthline.cling.model.meta.Service;
import org.fourthline.cling.model.types.ServiceId;
import org.fourthline.cling.support.avtransport.callback.GetMediaInfo;
import org.fourthline.cling.support.avtransport.callback.GetTransportInfo;
import org.fourthline.cling.support.avtransport.callback.Pause;
import org.fourthline.cling.support.avtransport.callback.Play;
import org.fourthline.cling.support.avtransport.callback.Seek;
import org.fourthline.cling.support.avtransport.callback.SetAVTransportURI;
import org.fourthline.cling.support.avtransport.callback.Stop;
import org.fourthline.cling.support.model.MediaInfo;
import org.fourthline.cling.support.model.TransportInfo;
import org.fourthline.cling.support.renderingcontrol.callback.GetVolume;
import org.fourthline.cling.support.renderingcontrol.callback.SetMute;
import org.fourthline.cling.support.renderingcontrol.callback.SetVolume;
import android.text.TextUtils;
public class Controller implements IController
{
private static final String AVTransport1 = "AVTransport";
private ControlPoint mControlPoint;
private static final String SetAVTransportURI = "SetAVTransportURI";
private static final String RenderingControl = "RenderingControl";
private static final String Play = "Play";
public Controller(ControlPoint conPoint)
{
super();
this.mControlPoint = conPoint;
}
@Override
public int getMaxVolumeValue(Device device)
{
String maxValue = getVolumeDbRange(device, "MaxValue");
if (TextUtils.isEmpty(maxValue))
{
return 100;
}
return Integer.parseInt(maxValue);
}
@Override
public String getMediaDuration(Device device)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (null == service)
{
return null;
}
ActionCallback callback = new GetMediaInfo(service)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void received(ActionInvocation arg0, MediaInfo arg1)
{
arg0.getAction();
String duration = arg1.getMediaDuration();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};
mControlPoint.execute(callback);
return null;
}
@Override
public int getMinVolumeValue(Device device)
{
String minValue = getVolumeDbRange(device, "MinValue");
if (TextUtils.isEmpty(minValue))
{
return 0;
}
return Integer.parseInt(minValue);
}
@Override
public String getMute(Device device)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (null == service)
{
return null;
}
/* ActionCallback callback=new GetMute(service)
{
@Override
public void failure(ActionInvocation paramActionInvocation, UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void received(ActionInvocation arg0, boolean arg1)
{
arg0.getAction();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};*/
final Action action = service.getAction("GetMute");
if (action == null)
{
return null;
}
ActionInvocation<Service> ai = new ActionInvocation<Service>(action);
ai.setInput("InstanceID", "0");
ai.setInput("Channel", "Master");
ActionCallback callback = new ActionCallback(ai)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
// TODO Auto-generated method stub
}
@Override
public void success(ActionInvocation paramActionInvocation)
{
action.getOutputArgument("CurrentMute");
}
};
mControlPoint.execute(callback);
return null;
}
@Override
public String getPositionInfo(Device device)
{
Service localService =
device.findService(ServiceId.valueOf(AVTransport1));
if (localService == null)
{
return null;
}
final Action localAction = localService.getAction("GetPositionInfo");
if (localAction == null)
{
return null;
}
ActionInvocation<Service> aci =
new ActionInvocation<Service>(localAction);
aci.setInput("InstanceID", "0");
ActionCallback callback = new ActionCallback(aci)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
// TODO Auto-generated method stub
}
@Override
public void success(ActionInvocation paramActionInvocation)
{
// TODO Auto-generated method stub
localAction.getInputArgument("AbsTime").getName();
}
};
mControlPoint.execute(callback);
return null;
}
@Override
public String getTransportState(Device device)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (null == service)
{
return null;
}
ActionCallback callback = new GetTransportInfo(service)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
// TODO Auto-generated method stub
}
@Override
public void received(ActionInvocation arg0, TransportInfo arg1)
{
// TODO Auto-generated method stub
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction()
.getOutputArgument("CurrentTransportState");
}
};
mControlPoint.execute(callback);
return null;
}
@Override
public int getVoice(Device device)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (null == service)
{
return -1;
}
ActionCallback callback = new GetVolume(service)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
// TODO Auto-generated method stub
}
@Override
public void received(ActionInvocation arg0, int arg1)
{
// TODO Auto-generated method stub
}
@Override
public void success(ActionInvocation arg0)
{
// TODO Auto-generated method stub
super.success(arg0);
}
};
mControlPoint.execute(callback);
return 0;
}
public String getVolumeDbRange(Device device, final String argument)
{
Service localService =
device.findService(ServiceId.valueOf(RenderingControl));
if (localService == null)
{
return null;
}
final Action action = localService.getAction("GetVolumeDBRange");
if (action == null)
{
return null;
}
ActionInvocation<Service> localAction =
new ActionInvocation<Service>(action);
localAction.setInput("InstanceID", "0");
localAction.setInput("Channel", "Master");
ActionCallback callback = new ActionCallback(localAction)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
// TODO Auto-generated method stub
}
@Override
public void success(ActionInvocation paramActionInvocation)
{
action.getOutputArgument(argument);
}
};
mControlPoint.execute(callback);
return null;
}
@Override
public boolean goon(Device device, String pausePosition)
{
Service localService =
device.findService(ServiceId.valueOf(AVTransport1));
if (localService == null)
{
return false;
}
final Action localAction = localService.getAction("Seek");
if (localAction == null)
{
return false;
}
ActionInvocation<Service> ain =
new ActionInvocation<Service>(localAction);
ain.setInput("InstanceID", "0");
// if (mUseRelTime) {
// } else {
// localAction.setArgumentValue("Unit", "ABS_TIME");
// }
// LogUtil.e(tag, "继续相对时间:"+mUseRelTime);
// 测试解决播放暂停后时间不准确
ain.setInput("Unit", "ABS_TIME");
ain.setInput("Target", pausePosition);
ActionCallback callback = new ActionCallback(ain)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
}
@Override
public void success(ActionInvocation paramActionInvocation)
{
}
};
mControlPoint.execute(callback);
ActionCallback playCallback = new Play(localService, "1")
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};
mControlPoint.execute(playCallback);
return false;
}
@Override
public boolean pause(Device device)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (service == null)
{
return false;
}
ActionCallback pauseCallback = new Pause(service)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
}
@Override
public void success(ActionInvocation invocation)
{
// TODO Auto-generated method stub
super.success(invocation);
}
};
mControlPoint.execute(pauseCallback);
return false;
}
@Override
public boolean play(Device device, String path)
{
//播放之前先stop 才可以播放新的歌曲
stop(device);
Service[] services = device.getServices();
final Service service =
device.findService(ServiceId.valueOf(AVTransport1));
if (service == null)
{
return false;
}
ActionCallback acb = new SetAVTransportURI(service, path)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
ActionCallback abc = new Play(service, "1")
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};
mControlPoint.execute(abc);//执行播放
}
};
mControlPoint.execute(acb);//URL
return true;
}
@Override
public boolean seek(Device device, String targetPosition)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (null == service)
{
return false;
}
ActionCallback callback = new Seek(service, targetPosition)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};
mControlPoint.execute(callback);
return true;
}
@Override
public boolean setMute(Device device, String targetValue)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (null == service)
{
return false;
}
ActionCallback callback = new SetMute(service, true)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};
mControlPoint.execute(callback);
return true;
}
@Override
public boolean setVoice(Device device, int value)
{
Service service =
device.findService(ServiceId.valueOf(RenderingControl));
if (null == service)
{
return false;
}
ActionCallback callback = new SetVolume(service, value)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};
mControlPoint.execute(callback);
return true;
}
@Override
public boolean stop(Device device)
{
Service service = device.findService(ServiceId.valueOf(AVTransport1));
if (null == service)
{
return false;
}
ActionCallback callback = new Stop(service)
{
@Override
public void failure(ActionInvocation paramActionInvocation,
UpnpResponse paramUpnpResponse, String paramString)
{
paramString.getBytes();
}
@Override
public void success(ActionInvocation invocation)
{
super.success(invocation);
invocation.getAction();
}
};
mControlPoint.execute(callback);
return true;
}
}
package com.yunning.wifiplay.dlna;
import org.fourthline.cling.model.meta.Device;
public interface IController {
/**
* Play the video with the video path.
*
* @param device
* The device be controlled.
* @param path
* The path of the video.
* @return If is success to play the video.
*/
boolean play(Device device, String path);
/**
* Go on playing the video from the position.
*
* @param device
* The device be controlled.
* @param pausePosition
* The format must be 00:00:00.
*/
boolean goon(Device device, String pausePosition);
/**
* All the state is "STOPPED" // "PLAYING" // "TRANSITIONING"//
* "PAUSED_PLAYBACK"// "PAUSED_RECORDING"// "RECORDING" //
* "NO_MEDIA_PRESENT//
*/
String getTransportState(Device device);
/**
* Get the min volume value,this must be 0.
*
* @param device
* @return
*/
int getMinVolumeValue(Device device);
/**
* Get the max volume value, usually it is 100.
*
* @param device
* The device be controlled.
* @return The max volume value.
*/
int getMaxVolumeValue(Device device);
/**
* Seek the playing video to a target position.
*
* @param device
* The device be controlled.
* @param targetPosition
* Target position we want to set.
* @return
*/
boolean seek(Device device, String targetPosition);
/**
* Get the current playing position of the video.
*
* @param device
* The device be controlled.
* @return Current playing position is 00:00:00
*/
String getPositionInfo(Device device);
/**
* Get the duration of the video playing.
*
* @param device
* The device be controlled.
* @return The media duration like 00:00:00,if get failed it will return
* null.
*/
String getMediaDuration(Device device);
/**
* Mute the device or not.
*
* @param device
* The device be controlled.
* @param targetValue
* 1 is that want mute, 0 if you want make it off of mute.
* @return If is success to mute the device.
*/
boolean setMute(Device device, String targetValue);
/**
* Get if the device is mute.
*
* @param device
* The device be controlled.
* @return 1 is mute, otherwise will return 0.
*/
String getMute(Device device);
/**
* Set the device's voice.
*
* @param device
* The device be controlled.
* @param value
* Target voice want be set.
* @return
*/
boolean setVoice(Device device, int value);
/**
* Get the current voice of the device.
*
* @param device
* The device be controlled.
* @return Current voice.
*/
int getVoice(Device device);
/**
* Stop to play.
*
* @param device
* The device to controlled.
* @return If if success to stop the video.
*/
boolean stop(Device device);
/**
* Pause the playing video.
*
* @param device
* The device to controlled.
* @return If if success to pause the video.
*/
boolean pause(Device device);
}
此代码用的cling开源库
转载请注明欢迎加入QQ群: 129837689。