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

时间:2018-02-16 10:34:12

标签: android server connection upnp dlna

我是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。

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,在深入了解一周以上的代码后,我基本了解了DLNA和UPNP的工作原理。如何进行回调以及如何从设备中获取媒体文件。所以,现在我能够回答我自己的问题。

如何获取非媒体文件:

每当发现服务找到您自己的设备时,它将准备媒体服务器,该服务器将从您的设备获取所有媒体。首先,它为特定类型的媒体创建节点。例如,视频,照片,音频等。以下是代码:

 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将成为您的文件路径,您可以从其他设备访问您的文件。您可以使用文件扩展名过滤文件。

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

谢谢!