关于DLNA开发Cling开源库的使用

关于DLNA开发Cling开源库的使用

          最近公司开发了一个多屏互动的应用,其中牵涉到了DLNA的应用而我有幸加入到了其中的开发之中,在网络上查找关于DLNA开发资料的时候看到网络上关于Cling开源库的使用只有一个叫做Wireme的源代码可以参考使用,但是该部分代码主要是实现了DMS对于其它部分的实现不是很完善。下面我是我为了让大家能更好的实现DLNA其它的部分特别是DMC的部分(主要涉及到控制DMR),将AVTransport部分独立出来写了一个通用的类,其中实现了大部分的DMC操作如暂停,播放,获取DMR播放设备的当前音量,获取DMR设备播放信息啊等等,下面就是我自己写的一个类有比较详细的注释,希望能给使用Cling开源库实现DNLA的朋友有一点点的帮助。

public class CoshipAvtransprot 
{
	/***********************************************************
	 * @param device
	 * @param Uri
	 * @param Title
	 *功能:设置mediarender播放的URI
	 ***********************************************************/
	public static void mediaRemendersetAVTransportURI(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, final String Uri, final String Brows,final String Title, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("AVTransport"));
		try
		{
			ActionCallback setAVTransportURIAction = new SetAVTransportURI(service, Uri, Brows) 
	        {
	        	@Override
				public void success(@SuppressWarnings("rawtypes") ActionInvocation invocation) 
	        	{
					super.success(invocation);
					System.out.println("设置URL成功");
			        getavtransport.setavtransportsuccess();
				}
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation invocation, UpnpResponse operation, String defaultMsg) 
	        	{
					getavtransport.setavtransportfail();
					System.out.println("设置URI失败");
	            }
	        };
	        upnpService.getControlPoint().execute(setAVTransportURIAction);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

	}
	
	
	
	/****************************************************************
	 * @param upnpService   
	 * @param device    mediarender设备名
	 * @param getavtransport   avtransport事件回调监听函数
	 * 功能:控制mediarender设备进行播放
	 */
	public static void mediaRemenderplay(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") final Device device, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		Service<?, ?> service = device.findService(new UDAServiceId("AVTransport"));
		try
		{
		  	ActionCallback playAction =   new Play(service) 
	    	{   	
	    		@Override
	    		public void success(@SuppressWarnings("rawtypes") ActionInvocation invocation) 
	    		{
	    			super.success(invocation);
	    			getavtransport.mediarenderplaysuccess();
	    			System.out.println("播放成功");
	    		}

	    		@Override            
	    		public void failure(@SuppressWarnings("rawtypes") ActionInvocation invocation, UpnpResponse operation, String defaultMsg) 
	    		{      
	    			System.out.println("播放失败");
	    			getavtransport.mediarenderplayfail();
	    		}        
	    	};
				upnpService.getControlPoint().execute(playAction);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	/***********************************************8
	 * @param upnpService
	 * @param device
	 * @param getGENASubscriptionStat
	 */
	public static void MeidaSubscription(AndroidUpnpService upnpService, final Device<?, ?, ?> device, final GetGENASubscriptionStat getGENASubscriptionStat)
	{
		if(device == null)
			return;
		Service<?, ?> service = device.findService(new UDAServiceId("AVTransport"));
		SubscriptionCallback callback = new SubscriptionCallback(service, 2000) {

			@Override
			protected void ended(@SuppressWarnings("rawtypes") GENASubscription arg0, CancelReason arg1,
					UpnpResponse arg2) 
			{
				System.out.println("ended");
			}

			@Override
			protected void established(@SuppressWarnings("rawtypes") GENASubscription arg0) 
			{
				System.out.println("established");
			}

			@Override
			protected void eventReceived(@SuppressWarnings("rawtypes") GENASubscription arg0) 
			{
				System.out.println("Event: " + arg0.getCurrentSequence().getValue());
				@SuppressWarnings({ "rawtypes", "unchecked" })
				Map<String, StateVariableValue> values = arg0.getCurrentValues();
				StateVariableValue<?> LastChange = values.get("LastChange");
				String result = "";
				result = CoshipUtils.parseLastChangeInfo(LastChange.toString());
				if(result.equals(CoshipUtils.REALPLAYING))
				{
					getGENASubscriptionStat.RealPlaying();
				}
				else if(result.equals(CoshipUtils.REALSTOPPED))
				{
					getGENASubscriptionStat.RealStopped();
				}
				else if(result.equals(CoshipUtils.REALSEEKSUCCESS))
				{
					getGENASubscriptionStat.RealSeekSuccess();
				}
				System.out.println("the current state is="+result);
			}

			@Override
			protected void eventsMissed(@SuppressWarnings("rawtypes") GENASubscription arg0, int arg1) 
			{
				System.out.println("eventsMissed");
			}

			@Override
			protected void failed(@SuppressWarnings("rawtypes") GENASubscription arg0, UpnpResponse arg1,
					Exception arg2, String arg3) 
			{
				System.out.println("failed");
			}
		    };
		    upnpService.getControlPoint().execute(callback);
	}
	
	
	
	
	
	
	
	/*****************************************************
	 * @param upnpService
	 * @param device   mediarender设备名
	 * @param volume   设置声音大小
	 * @param getavtransport     avtransport事件回调监听函数
	 * 功能:设置meidarender设备声音大小
	 */
	public static void setvolume(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, int volume, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		 Service<?, ?> service = device.findService(new UDAServiceId("RenderingControl"));
		 Action<?> getStatusAction = service.getAction("SetVolume");
		 System.out.println("VOLUME="+volume);
		 @SuppressWarnings({ "unchecked", "rawtypes" })
		ActionInvocation<?> getStatusInvocation = new ActionInvocation(getStatusAction);
		 try
		 {
			 ActionCallback setvolume = new SetVolume(getStatusInvocation, volume)
			 {
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1,
						String arg2) 
				{
					System.out.println("设置当前音量大小失败");
					super.failure(arg0, arg1, arg2);
					getavtransport.mediarendersetvolumefail();
				}

				@Override
				public void success(@SuppressWarnings("rawtypes") ActionInvocation arg0) 
				{			
					System.out.println("设置当前音量大小成功");
					//获取当前音量状态成功
					super.success(arg0);
					getavtransport.mediarendersetvolumesuccess();
				} 
			 };
			 upnpService.getControlPoint().execute(setvolume);	
		 }
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }
	}
	
	
	
	/*************************************************************
	 * 
	 * @param upnpService
	 * @param device             mediarender设备名
	 * @param getavtransport     avtransport事件回调监听函数
	 * 功能:获取mediarender设备是否静音
	 */
	public static void getmute(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		 Service<?, ?> service = device.findService(new UDAServiceId("RenderingControl"));
		 @SuppressWarnings("rawtypes")
		 Action getStatusAction = service.getAction("GetMute");
		 @SuppressWarnings({ "rawtypes", "unchecked" })
		 ActionInvocation getStatusInvocation = new ActionInvocation(getStatusAction);
		 try
		 {
			 ActionCallback getmute = new GetMute(getStatusInvocation)
			 {
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1,
						String arg2) 
				{
					System.out.println("获取当前音量状态失败");
					super.failure(arg0, arg1, arg2);
				}

				@Override
				public void success(@SuppressWarnings("rawtypes") ActionInvocation arg0) 
				{				
					System.out.println("获取当前音量状态成功");
					//获取当前音量状态成功
					super.success(arg0);
					@SuppressWarnings("rawtypes")
					ActionArgumentValue[] action= arg0.getOutput();
					for(int i = 0; i < action.length; i++)
					{
						System.out.println(action[i].toString());
					}
				} 
			 };
			 upnpService.getControlPoint().execute(getmute);	
		 }
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }
	}
	
	
	
	/**************************************************
	 * @param upnpService   
	 * @param device              mediarender设备名
	 * @param mute                是否静音标志
	 * @param getavtransport      avtransport事件回调监听函数
	 * 功能:   设置mediarender是否静音
	 */
	public static void setmute(AndroidUpnpService upnpService,@SuppressWarnings("rawtypes") Device device, int mute, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("RenderingControl"));
		@SuppressWarnings("rawtypes")
		Action getStatusAction = service.getAction("SetMute");
		@SuppressWarnings({ "rawtypes", "unchecked" })
		ActionInvocation getStatusInvocation = new ActionInvocation(getStatusAction);
		 try
		 {
			 ActionCallback setmute = new SetMute(getStatusInvocation, mute)
			 {
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1,
						String arg2) 
				{
					System.out.println("设置当前音量状况失败");
				}

				@Override
				public void success(@SuppressWarnings("rawtypes") ActionInvocation arg0) 
				{
					System.out.println("设置当前音量状况成功");
				}
			 };
			 upnpService.getControlPoint().execute(setmute);	
		 }
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }
	}
	
	
	
	
	/***************************************************
	 * @param upnpService
	 * @param device              mediarender设备名
	 * @param volume              声音大小
	 * @param getavtransport      avtransport事件回调监听函数
	 * 功能:获取mediarender设备声音大小
	 */
	public static void getvolume(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, int volume, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("RenderingControl"));
		@SuppressWarnings("rawtypes")
		Action getStatusAction = service.getAction("GetVolume");
		@SuppressWarnings({ "rawtypes", "unchecked" })
		ActionInvocation getStatusInvocation = new ActionInvocation(getStatusAction);
		 try
		 {
			 ActionCallback getvolume = new GetVolume(getStatusInvocation) {
				
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1, String arg2) 
				{
				}
				
				@SuppressWarnings("unused")
				public void received(@SuppressWarnings("rawtypes") ActionInvocation arg0, int arg1) 
				{
	
				}
			};
			 upnpService.getControlPoint().execute(getvolume);	
		 }
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }
	}
	
	
	
	
    /************************************************888
     * @param upnpService
     * @param device            mediarender设备名
     * @param getavtransport    avtransport事件回调监听函数
     * 功能:设置mediarender暂停播放
     */
	public static void mediaRenderpause(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("AVTransport"));
		try
		{	
		  	ActionCallback pauseAction =   new Pause(service) 
	    	{   	
	    		@Override
	    		public void success(@SuppressWarnings("rawtypes") ActionInvocation invocation) 
	    		{
	    			super.success(invocation);
	    		    getavtransport.mediarenderpausesuccess();
	    		}

	    		@Override            
	    		public void failure(@SuppressWarnings("rawtypes") ActionInvocation invocation, UpnpResponse operation, String defaultMsg) 
	    		{                
	    			getavtransport.mediarenderpausefail();
	    		}        
	    	};	
	    	upnpService.getControlPoint().execute(pauseAction);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	
	

	/*******************************************************
	 * @param upnpService
	 * @param device               mediarender设备名
	 * @param time                 seektime时间
	 * @param getavtransport       avtransport事件回调监听函数
	 * 功能:设置mediarender进行seek播放
	 */
	public static void mediaRenderSeek(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, String time, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("AVTransport"));
		try
		{			
			ActionCallback seekAction = new Seek(service, time) 
			{			
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1, String arg2) 
				{
					getavtransport.mediarenderseekfail();
				}

				@Override
				public void success(@SuppressWarnings("rawtypes") ActionInvocation invocation) 
				{
					super.success(invocation);
					getavtransport.mediarenderseeksuccess();

				}
			};
			upnpService.getControlPoint().execute(seekAction);		
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	
	/************************************************************
	 * @param device
	 * 功能:停止播放当前音乐或者视频内容
	 ***********************************************************/
	public static void mediaRemenderstop(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") final Device device, final GetCoshipavtransportstate getavtransport)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("AVTransport"));
		try
		{
			ActionCallback stopAction = new Stop(service)
			{
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1,
						String arg2) 
				{
					getavtransport.mediarenderstopfail();
				}

				@Override
				public void success(@SuppressWarnings("rawtypes") ActionInvocation invocation) 
				{
					super.success(invocation);
					getavtransport.mediarenderstopsuccess();
				}
			};	
				upnpService.getControlPoint().execute(stopAction);

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	/********************************************************************8
	 * @param upnpService
	 * @param device                 mediarender设备名
	 * @param getmediarenderinfo     获取mediainfo回调监听函数
	 * 功能:获取mediarender的媒体信息
	 */
	public static void GetMediaInfo(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, final Getmediarenderinfo getmediarenderinfo)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("AVTransport"));
		try
		{
			ActionCallback getmediainfo = new GetMediaInfo(service) 
			{		
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1, String arg2) 
				{		
					getmediarenderinfo.getmediarendermediainfofail();
				}
				
				@Override
				public void received(@SuppressWarnings("rawtypes") ActionInvocation invocation, MediaInfo mediaInfo) 
				{
					getmediarenderinfo.getmediarendermediainfosuccess(mediaInfo);
				}
			};
			upnpService.getControlPoint().execute(getmediainfo);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	/*********************************************************************** 
	 * @param upnpService
	 * @param device               mediarender设备名     
	 * @param getmediarenderinfo   获取mediainfo回调监听函数
	 * 功能:获取mediarender的播放位置信息
	 */
	public static void GetPositionInfo(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device, final Getmediarenderinfo getmediarenderinfo)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("AVTransport"));
		try
		{
			ActionCallback getpositionInfo = new GetPositionInfo(service) 
			{
				@Override
				public void failure(@SuppressWarnings("rawtypes") ActionInvocation arg0, UpnpResponse arg1, String arg2) 
				{		
					System.out.println("GetPositionInfo failure");
					getmediarenderinfo.getmediarenderposinfofail();
				}
				
				@Override
				public void received(@SuppressWarnings("rawtypes") ActionInvocation invocation, PositionInfo positionInfo) 
				{		
					getmediarenderinfo.getmediarenderposinfosuccess(positionInfo);
				}
			};
				upnpService.getControlPoint().execute(getpositionInfo);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	
	/*********************************************
	 * @param upnpService  upnp服务
	 * @param device       当前和客户端连接的DMR设备
	 * 功能:获取DMR当前的状态
	 *********************************************/
	public static void GetDmrTransportInfo(AndroidUpnpService upnpService, @SuppressWarnings("rawtypes") Device device)
	{
		if(device == null)
			return;
		@SuppressWarnings("rawtypes")
		Service service = device.findService(new UDAServiceId("AVTransport"));
		ActionCallback getTransportInfo = new GetTransportInfo(service)
		{
			@Override
			public void received(@SuppressWarnings("rawtypes") ActionInvocation invocation,
					TransportInfo transportInfo) {
				System.out.println("transportInfo="+transportInfo.getCurrentTransportState());
				transportInfo.getCurrentTransportState();
			}
			@Override
			public void failure(@SuppressWarnings("rawtypes") ActionInvocation invocation,
					UpnpResponse operation, String defaultMsg) 
			{

			}
		};
		upnpService.getControlPoint().execute(getTransportInfo);
	}
}


你可能感兴趣的:(关于DLNA开发Cling开源库的使用)