android DLNA之dmp开发详细介绍

 DLNA详细情况可详见百度

DMP数字媒体播放器,能从DMS/M-DMS上查找并获取媒体内容并播放和渲染显示

废话不多说。。关于dmp代码wireme已经很详细了,今天记录一下这几天的理解。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.v(TAG, "CreateView");
		mMainView = inflater
				.inflate(R.layout.fragment_device, container, false);
		getActivity().bindService(new Intent(context, WireUpnpService.class),
				serviceConnection, Context.BIND_AUTO_CREATE);
		init();
		return mMainView;
	}
多余的代码不用管,可以看到在oncreate的时候我们绑定了一个服务, WireUpnpService.class这里面没有东西,主要注意一下serviceConnection,让我们看下serviceConnection的代码.

	private ServiceConnection serviceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName className, IBinder service) {
			// 初始化
			upnpService = (AndroidUpnpService) service;
			// 绑定数据之前先解除绑定,移除本地设备信息,移除远程设备信息
			upnpService.getRegistry().removeAllLocalDevices();
			upnpService.getRegistry().removeAllRemoteDevices();
			if (mediaServer == null) {
				// try {
				// mediaServer = new MediaServer(getLocalIpAddress());
				// } catch (Exception e) {
				// Log.v(TAG, e.getMessage());
				// e.printStackTrace();
				// }

				mediaServer = WireUpnpService.mediaServer;
			}
			// 添加本地手机信息
			prepareMediaServer();
			deviceItems.clear();
			// 注册服务添加设备
			upnpService.getRegistry().addDevice(mediaServer.getDevice());
			// 添加本机设备信息至DeviceItem
			for (Device device : upnpService.getRegistry().getDevices()) {
				deviceListRegistryListener.deviceAdded(new DeviceItem(device));
			}
			// 添加监听
			upnpService.getRegistry().addListener(deviceListRegistryListener);
			// 搜索周边设备
			upnpService.getControlPoint().search();

		}

		public void onServiceDisconnected(ComponentName className) {
			upnpService = null;
		}
	};


 
  注释一下怕自己忘记。。可以注意到关键的服务MediaServer,和关键的监听器DeviceListRegistryListener,接下来让我们看看MediaServer的代码 
  

public class MediaServer {

	private UDN udn = UDN.uniqueSystemIdentifier("GNaP-MediaServer");
	private LocalDevice localDevice;
	private final static String deviceType = "MediaServer";
	private final static int version = 1;
	private final static String LOGTAG = "GNaP-MediaServer";
	private final static int port = 8192;
	private static InetAddress localAddress;
	private static final UnsignedIntegerFourBytes sInstanceId = new UnsignedIntegerFourBytes(
			0);
	private AVTransportService avTransportService = null;
	private AVTransport avTransport = null;
	private Context context;

	public MediaServer(InetAddress localAddress) throws ValidationException {

		DeviceType type = new UDADeviceType(deviceType, version);
		// MODEL 手机型号 MANUFACTURER 手机厂商
		DeviceDetails details = new DeviceDetails(android.os.Build.MODEL,
				new ManufacturerDetails(android.os.Build.MANUFACTURER),
				new ModelDetails("GNaP", "GNaP MediaServer for Android", "v1"));

		// 读取LocalService元数据注释
		LocalService service = new AnnotationLocalServiceBinder()
				.read(ContentDirectoryService.class);
		// 绑定服务
		service.setManager(new DefaultServiceManager(
				service, ContentDirectoryService.class));
		// 本地设备
		localDevice = new LocalDevice(new DeviceIdentity(udn), type, details,
				service);

		this.localAddress = localAddress;

		this.context = context;
		Log.v(LOGTAG, "MediaServer device created: ");
		Log.v(LOGTAG, "friendly name: " + details.getFriendlyName());
		Log.v(LOGTAG, "manufacturer: "
				+ details.getManufacturerDetails().getManufacturer());
		Log.v(LOGTAG, "model: " + details.getModelDetails().getModelName());

		// start http server
		try {
			new HttpServer(port);
		} catch (IOException ioe) {
			System.err.println("Couldn't start server:\n" + ioe);
			System.exit(-1);
		}

		Log.v(LOGTAG, "Started Http Server on port " + port);
	}

	public LocalDevice getDevice() {
		return localDevice;
	}

	public String getAddress() {
		Log.v(LOGTAG, "test:" + localAddress.getHostAddress() + ":" + port);
		return localAddress.getHostAddress() + ":" + port;
	}

}
DeviceDetails details = new DeviceDetails(android.os.Build.MODEL,
				new ManufacturerDetails(android.os.Build.MANUFACTURER),
				new ModelDetails("GNaP", "GNaP MediaServer for Android", "v1"));

这句代码定义了本地设备的详细信息

	LocalService service = new AnnotationLocalServiceBinder()
				.read(ContentDirectoryService.class);
		// 绑定服务
		service.setManager(new DefaultServiceManager(
				service, ContentDirectoryService.class));
这俩句话就是绑定了MediaServer服务


// 本地设备
		localDevice = new LocalDevice(new DeviceIdentity(udn), type, details,
				service);
这句话就等于把本地的信息绑定起来,也就让别的dlna设备可以搜索到

接下来我们看下关键的DeviceListRegistryListener代码:

	class DeviceListRegistryListener extends DefaultRegistryListener {


		/* Discovery performance optimization for very slow Android devices! */


		@Override
		public void remoteDeviceDiscoveryStarted(Registry registry,
				RemoteDevice device) {
		}


		@Override
		public void remoteDeviceDiscoveryFailed(Registry registry,
				final RemoteDevice device, final Exception ex) {
			// deviceRemoved(device);


		}


		/*
		 * End of optimization, you can remove the whole block if your Android
		 * handset is fast (>= 600 Mhz)
		 */


		@Override
		public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
			// 搜索附近符合的设备
			if (device.getType().getNamespace().equals("schemas-upnp-org")
					&& device.getType().getType().equals("MediaServer")) {
				final DeviceItem display = new DeviceItem(device, device
						.getDetails().getFriendlyName(),
						device.getDisplayString(), "(REMOTE) "
								+ device.getType().getDisplayString());
				deviceAdded(display);


			}


		}


		@Override
		public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
			final DeviceItem display = new DeviceItem(device,
					device.getDisplayString());
			deviceRemoved(display);


		}


		@Override
		public void localDeviceAdded(Registry registry, LocalDevice device) {
			final DeviceItem display = new DeviceItem(device, device
					.getDetails().getFriendlyName(), device.getDisplayString(),
					"(REMOTE) " + device.getType().getDisplayString());
			Log.v("DeviceDisplay", display + "");
			//添加本地设备
			deviceAdded(display);
		}


		@Override
		public void localDeviceRemoved(Registry registry, LocalDevice device) {
			final DeviceItem display = new DeviceItem(device,
					device.getDisplayString());
			//移除设备
			deviceRemoved(display);
		}


		public void deviceAdded(final DeviceItem di) {
			if (getActivity() != null) {
				getActivity().runOnUiThread(new Runnable() {
					public void run() {


						// System.out.println(di);
						int position = deviceListAdapter.getPosition(di);
						if (position >= 0) {
							// Device already in the list, re-set new value
							// at
							// same
							// position
							deviceItems.remove(di);
							deviceItems.add(position, di);
						} else {
							deviceItems.add(di);
						}
						deviceListAdapter.notifyDataSetChanged();


						handler.sendEmptyMessage(0);


						// Sort it?
						// listAdapter.sort(DISPLAY_COMPARATOR);
						// listAdapter.notifyDataSetChanged();
					}


				});
			}


		}


		public void deviceRemoved(final DeviceItem di) {
			getActivity().runOnUiThread(new Runnable() {
				public void run() {
					deviceItems.remove(di);
					deviceListAdapter.notifyDataSetChanged();
				}
			});
		}


	}

。。。目前的理解就是这样了,dmr,dmc就看情况写

记录一下

你可能感兴趣的:(android DLNA之dmp开发详细介绍)