DLNA/UPNP - 获取所有非媒体文件

DLNA/UPNP - Get all non-media files

我是 UpNp 和 DLNA 的新手。我正在使用来自 here 的 Cling 库和 DLNA 代码。在这段代码中,一切都完美无缺,但我的需要是别的东西。它为我提供了网络中选定设备的所有媒体文件夹(视频、音频和图像)。但我想要的是来自网络上选定设备的非媒体文件,即 .docx、.srt、.txt 等。

这是一些代码部分:

 upnpService.getControlPoint().execute(
                new ContentBrowseActionCallback(ContentActivity.this,
                        service, createRootContainer(service), mContentList,
                        mHandler));

根容器:

 protected Container createRootContainer(Service service) {
        Container rootContainer = new Container();
        rootContainer.setId("0");
        rootContainer.setTitle("Content Directory on "
                + service.getDevice().getDisplayString());
        Log.e("createRootContainer", "service.getDevice().getDisplayString() : " + service.getDevice().getDisplayString());
        return rootContainer;
    }

ContentBrowseActionCallback:

public class ContentBrowseActionCallback extends Browse {

    private static Logger log = Logger
            .getLogger(ContentBrowseActionCallback.class.getName());

    private Service service;

    private Container container;

    private ArrayList<ContentItem> list;

    private Activity activity;

    private Handler handler;

    public ContentBrowseActionCallback(Activity activity, Service service,
                                       Container container, ArrayList<ContentItem> list, Handler handler) {
        super(service, container.getId(), BrowseFlag.DIRECT_CHILDREN, "*", 0,
                null, new SortCriterion(true, "dc:title"));
        this.activity = activity;
        this.service = service;
        this.container = container;
        this.list = list;
        this.handler = handler;
    }

    public void received(final ActionInvocation actionInvocation,
                         final DIDLContent didl) {
        log.fine("Received browse action DIDL descriptor, creating tree nodes");
        activity.runOnUiThread(new Runnable() {
            public void run() {
                try {
                    list.clear();
                    // Containers first
                    for (Container childContainer : didl.getContainers()) {
                        Log.e("Item", "childContainer : " + childContainer.getTitle());
                        log.fine("add child container "
                                + childContainer.getTitle());
                        list.add(new ContentItem(childContainer, service));
                    }
                    // Now items
                    for (Item childItem : didl.getItems()) {
                        Log.e("Item", "childItem : " + childItem.getTitle());
                        log.fine("add child item" + childItem.getTitle());
                        list.add(new ContentItem(childItem, service));
                    }
                } catch (Exception ex) {
                    log.fine("Creating DIDL tree nodes failed: " + ex);
                    actionInvocation.setFailure(new ActionException(
                            ErrorCode.ACTION_FAILED,
                            "Can't create list childs: " + ex, ex));
                    failure(actionInvocation, null);
                    handler.sendEmptyMessage(ContentActivity.CONTENT_GET_FAIL);
                }
                ConfigData.listPhotos.clear();
                Iterator iterator = didl.getItems().iterator();
                while (iterator.hasNext()) {
                    Item item = (Item) iterator.next();
                    Log.e("received", "item.getTitle() : " + item.getTitle());
                    ContentItem contentItem = new ContentItem(item,
                            ContentBrowseActionCallback.this.service);
                    if ((contentItem.getItem().getTitle().toString() != null)
                            && (contentItem.getItem().getResources() != null)) {

                        List list = contentItem.getItem().getResources();
                        if ((list.size() != 0)
                                && (((Res) list.get(0)).getProtocolInfo() != null)
                                && (((Res) list.get(0)).getProtocolInfo()
                                .getContentFormat() != null)) {
                            Log.e("received", "list.get(0).getProtocolInfo() : " + ((Res) list.get(0)).getProtocolInfo());
                            Log.e("received", "list.get(0).getProtocolInfo().getContentFormat() : " + ((Res) list.get(0)).getProtocolInfo().getContentFormat());
                            Log.e("received", "list.get(0).getProtocolInfo().getContentFormat().substring() : " + ((Res) list.get(0)).getProtocolInfo().getContentFormat().substring(0,
                                    ((Res) list.get(0))
                                            .getProtocolInfo()
                                            .getContentFormat()
                                            .indexOf("/")));
                            if (((Res) list.get(0))
                                    .getProtocolInfo()
                                    .getContentFormat()
                                    .substring(
                                            0,
                                            ((Res) list.get(0))
                                                    .getProtocolInfo()
                                                    .getContentFormat()
                                                    .indexOf("/"))
                                    .equals("image")) {
                                ConfigData.listPhotos
                                        .add(new ContentItem(
                                                item,
                                                ContentBrowseActionCallback.this.service));
                            } else if (((Res) list.get(0))
                                    .getProtocolInfo()
                                    .getContentFormat()
                                    .substring(
                                            0,
                                            ((Res) list.get(0))
                                                    .getProtocolInfo()
                                                    .getContentFormat()
                                                    .indexOf("/"))
                                    .equals("audio")) {
                                ConfigData.listAudios
                                        .add(new ContentItem(
                                                item,
                                                ContentBrowseActionCallback.this.service));
                            } else {
                                Log.e("received", "item : " + item.getTitle());
                                ConfigData.listVideos
                                        .add(new ContentItem(
                                                item,
                                                ContentBrowseActionCallback.this.service));
                            }
                        }
                    }
                }
                handler.sendEmptyMessage(ContentActivity.CONTENT_GET_SUC);
            }
        });
    }

    public void updateStatus(final Status status) {
    }

    @Override
    public void failure(ActionInvocation invocation, UpnpResponse operation,
                        final String defaultMsg) {
    }
}

关于这一点,我在谷歌上搜索了很多,但手头没有。我对容器 ID 及其工作原理知之甚少。

请让我知道如何实现这种情况。我也不太了解 DLNA 和 UPNP 的工作原理。也请帮助我更好地理解DLNA和UPNP。

谢谢!

好的,在深入研究代码一个多星期后,我对 DLNA 和 UPNP 的工作原理有了基本的了解。如何进行回调以及如何从设备中获取媒体文件。所以,现在我可以回答我自己的问题了。

如何获取非媒体文件:

只要发现服务发现您自己的设备,它就会准备媒体服务器,从您的设备中获取所有媒体。首先,它为特定类型的 media.e.g.、视频、照片、音频等创建一个节点。代码如下:

 ContentNode rootNode = ContentTree.getRootNode();
 // Video Container
 Container videoContainer = new Container();
 videoContainer.setClazz(new DIDLObject.Class("object.container"));
 videoContainer.setId(ContentTree.MY_FOLDER_ID);
 videoContainer.setParentID(ContentTree.ROOT_ID);
 videoContainer.setTitle("My Folder Name");
 videoContainer.setRestricted(true);
 videoContainer.setWriteStatus(WriteStatus.NOT_WRITABLE);
 videoContainer.setChildCount(0);
 rootNode.getContainer().addContainer(videoContainer);
 rootNode.getContainer().setChildCount(
              rootNode.getContainer().getChildCount() + 1);
 ContentTree.addNode(ContentTree.MY_FOLDER_ID, new ContentNode(
                ContentTree.MY_FOLDER_ID, videoContainer));

这样你就可以添加自己的文件夹了。

要获取文件,您需要使用 Cursor 从您的设备获取文件。像这样:

String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
Uri externalUri = MediaStore.Files.getContentUri("external");
        String[] filePathColumn = {MediaStore.Files.FileColumns._ID, 
         MediaStore.Files.FileColumns.DATA};
Cursor cursor = contentResolver.query(externalUri, filePathColumn, 
                selection, null, null);
cursor.moveToFirst();
do {
   String id = 
         cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
   String path = 
         cursor.getString(cursor.getColumnIndex(filePathColumn[1]));
   String serverPath = "http://" + mediaServer.getAddress() + "/" 
         +path;
} while (cursor.moveToNext());
cursor.close();

因此,serverPath 将成为您的文件路径,您可以从其他设备访问您的文件。您可以使用文件扩展名过滤文件。

我必须说这是我正在使用的一个巨大的项目,所以我做了很多改变来实现我的要求。但是,这个答案证明了我的问题。

谢谢!