A example project for cast media file(Image, Video, Audio) from android device to windows computer by UPNP protocol.
-
Add repository for download cling and jetty package.
maven { allowInsecureProtocol(true) url "http://4thline.org/m2/" }
-
Implement dependencies.
implementation 'org.fourthline.cling:cling-core:2.1.1' implementation 'me.shaohui:bottomdialog:1.1.9' // for device choose implementation 'io.github.xxmd:upnp-cast:1.0.0'
-
Add required permission, service and cast activity in Androidminifaset.xml file.
<!--Required permission--> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <service android:name="org.fourthline.cling.android.AndroidUpnpServiceImpl"/> <!--Cast entry activity--> <activity android:name="io.github.xxmd.CastActivity"/>
-
Main code steps.
private void doAfterPermissionGranted(Runnable runnable, String... permission) { RxPermissions rxPermissions = new RxPermissions(getActivity()); rxPermissions.request(permission) .subscribe(granted -> { if (granted) { runnable.run(); } else { // show tips } }); } private void chooseDevice() { String[] requiredPermissions = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, Manifest.permission.WAKE_LOCK, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_FINE_LOCATION}; doAfterPermissionGranted(() -> { SearchDeviceDialog searchDeviceDialog = new SearchDeviceDialog(selectedDevice); searchDeviceDialog.setSearchDeviceListener(new SearchDeviceDialog.SearchDeviceListener() { @Override public void onConfirm(Device device) { // echo device info } @Override public void onCancel() { } }); searchDeviceDialog.show(getActivity().getSupportFragmentManager()); }, requiredPermissions); } private void chooseFile(Consumer<List<String>> onSuccess) { // implement by yourself, please make sure the result files all have same type(all image or all video or all audio). } /** * 1. check selectedDevice is existed. * 2. choose cast files. * 3. start cast activity. */ private void chooseFileThenCast() { if (selectedDevice != null) { String[] fileRelatedPermission = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; doAfterPermissionGranted(() -> { chooseFile(filePathList -> { CastActivity.startCastActivity(mContext, selectedDevice, filePathList); }); }, fileRelatedPermission); } }
-
Want to view more usage details please clone the project then run it.
-
Start the Windows Media Player application on windows.
-
Make sure you have checked two necessary options when you first use.
- Allow control my player remotely.
- Allow auto play my media.
-
Run upnp-cast application, choose device firstly, then cast any media file by yourself.
-
You will see visualize result on compute screen when cast successfully.
Cast image | Cast video | Cast audio |
---|---|---|
Some compute cannot cast audio successfully due to lack audio output device(headset
, speaker
, etc...).