關於DLNA開發Cling開源庫的使用

關於DLNA開發Cling開源庫的使用

          最近公司開發了一個多屏互動的應用,其中牽涉到了DLNA的應用而我有幸加入到了其中的開發之中,在網絡上查找關於DLNA開發資料的時候看到網絡上關於Cling開源庫的使用只有一個叫作Wireme的源代碼能夠參考使用,可是該部分代碼主要是實現了DMS對於其它部分的實現不是很完善。下面我是我爲了讓你們能更好的實現DLNA其它的部分特別是DMC的部分(主要涉及到控制DMR),將AVTransport部分獨立出來寫了一個通用的類,其中實現了大部分的DMC操做如暫停,播放,獲取DMR播放設備的當前音量,獲取DMR設備播放信息啊等等,下面就是我本身寫的一個類有比較詳細的註釋,但願能給使用Cling開源庫實現DNLA的朋友有一點點的幫助。java

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);
	}
}


本文分享 CSDN - IT先森。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。網絡

相關文章
相關標籤/搜索