通过Cling从媒体服务器获取要播放的媒体的URL

时间:2012-10-22 21:52:34

标签: android upnp

我正在研究Android上Cling upnp堆栈的示例upnpBrowser,我浏览merdia服务器的内容,我可以在每个文件夹中显示媒体列表,现在我想提取要播放的视频的URL它在播放器上,但我阻止了如何提取该URL!

UDN serverUdn = ((UpnpControlApplication)getApplicationContext()).server_udn;
//Device server = ((UpnpControlApplication)getApplicationContext()).server;
Device server = upnpService.getRegistry().getDevice(serverUdn, true);
listAdapter.clear();
if(server == null)
{
    Toast.makeText(this, "pbm", Toast.LENGTH_LONG).show();
}
else
{
    if(server.findService(new UDAServiceType("ContentDirectory")) == null)
        Toast.makeText(this, "pbm", Toast.LENGTH_LONG).show();
    upnpService.getControlPoint().execute(new Browse(server.findService(new UDAServiceType("ContentDirectory")),id, BrowseFlag.DIRECT_CHILDREN)
    {
        @Override
        public void received(ActionInvocation actionInvocation, DIDLContent didl) {
            final List<Item> items = didl.getItems();
            String st=didl.getItems().get(0).getFirstResource().getValue();

2 个答案:

答案 0 :(得分:1)

public void browseServer(final String id)
{

    if(((UpnpControlApplication)getApplicationContext()).server_udn == null)
    {
        new AlertDialog.Builder(this)
        .setTitle("Bad")
        .setMessage("pas de serveur sélectionné, allez à la section LAN et cliquez sur Parcourir pour sélectionner le serveur!")
        .setPositiveButton("OK", new OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // Some stuff to do when ok got clicked
            }
        })
        .setNegativeButton("Annuler", new OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // Some stuff to do when cancel got clicked
            }
        })
        .show();
    }
    else
    {
        UDN serverUdn = ((UpnpControlApplication)getApplicationContext()).server_udn;
        //Device server = ((UpnpControlApplication)getApplicationContext()).server;
        Device server = upnpService.getRegistry().getDevice(serverUdn, true);
        listAdapter.clear();
        if(server == null)
        {
            Toast.makeText(this, "désolé", Toast.LENGTH_LONG).show();
        }
        else
        {
            if(server.findService(new UDAServiceType("ContentDirectory")) == null)
                Toast.makeText(this, "pbm", Toast.LENGTH_LONG).show();
            upnpService.getControlPoint().execute(new Browse(server.findService(new UDAServiceType("ContentDirectory")),id, BrowseFlag.DIRECT_CHILDREN)
            {

                @Override
                public void received(ActionInvocation actionInvocation, DIDLContent didl) {

                    final List<Item> items = didl.getItems();

                    //String st = items.getImportUri();
                    //String st=didl.getItems().get(0).getFirstResource().getValue(); 
                    //Log.d("URL IS",st); 
                    //Item item = didl.getItems().get(0);
                    //Item item = didl.getItems().get(0); 
                    //String url = item.getFirstResource().getValue();

                     //System.out.println(" this is the url of item" +item.getTitle()); 
                    // System.out.println(" this is the url of the media");

                     //System.out.println(url);

                    /*final DIDLContent b = didl;
                    String str1 = "bieda";
                    try
                    {
                        str1 = parser.generate(b);
                    }
                    catch(Exception e)
                    {
                        str1 = "smuta";
                    }
                    final String str=str1;
                    runOnUiThread(new Runnable()
                    {
                        public void run()
                        {

                            adb.setTitle("XML").setMessage(str).setPositiveButton("OK", new OnClickListener() {
                                public void onClick(DialogInterface arg0, int arg1) {
                                    // Some stuff to do when ok got clicked
                                }
                            }).show();

                        }
                    });*/

                    runOnUiThread(new Runnable(){
                        public void run()
                        {
                            listAdapter.insert(new ServerContentContainer(s.peek()), 0);
                        }
                    });

                    final List<Container> containers = didl.getContainers();

                    for(final Container c: containers)
                    {

                        runOnUiThread(new Runnable(){
                            public void run()
                            {
                                int pos = listAdapter.getPosition(new ServerContentContainer(c));
                                if(pos >= 0)
                                {
                                    listAdapter.remove(new ServerContentContainer(c));
                                    listAdapter.insert(new ServerContentContainer(c), pos);
                                    System.out.println("eulooooooooo");
                                }
                                else
                                    listAdapter.add(new ServerContentContainer(c));
                            }
                        });
                    }
                    for(final Item i: items)
                    {
                        runOnUiThread(new Runnable(){
                            public void run()
                            {
                                int pos = listAdapter.getPosition(new ServerContentItem(i));
                                if(pos >= 0)
                                {
                                    listAdapter.remove(new ServerContentItem(i));
                                    listAdapter.insert(new ServerContentItem(i), pos);
                                }
                                else
                                    listAdapter.add(new ServerContentItem(i));
                            }
                        });

                    }

                }

                @Override
                public void updateStatus(Status arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void failure(ActionInvocation arg0, UpnpResponse arg1,
                        String arg2) {
                    // TODO Auto-generated method stub

                }

            });
        }
    }
}

这是类ServerContentItem:

protected class ServerContentItem extends ServerContent
    {
        private Item i;

        public Item getItem()
        {
            return i;
        }

        ServerContentItem(Item i)
        {
            this.i = i;
        }

        public void select()
        {
            if(((UpnpControlApplication)getApplicationContext()).renderer_udn != null)
            {
                Device renderer = upnpService.getRegistry().getDevice(((UpnpControlApplication)getApplicationContext()).renderer_udn, false);
                final Service service = renderer.findService(new UDAServiceType("AVTransport"));
                upnpService.getControlPoint().execute(new SetAVTransportURI(service, i.getFirstResource().getValue()){
                    @Override
                    public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
                        adb.setTitle("Bad").setMessage("No!")
                        .setPositiveButton("OK",new OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                                // Some stuff to do when ok got clicked
                            }
                        }).show();
                    }

                    @Override
                    public void success(ActionInvocation invocation)
                    {
                        upnpService.getControlPoint().execute(new Play(service){
                            @Override
                            public void failure(ActionInvocation invocation, UpnpResponse operation, String defaultMsg) {
                                adb.setTitle("Bad").setMessage("Non réussi!")
                                .setPositiveButton("OK",new OnClickListener() {
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        // Some stuff to do when ok got clicked
                                    }
                                }).show();
                            }
                        });
                    }
                });

            }
            else
            {
                adb.setTitle("Bad")
                .setMessage("selection ! need a media player ")
                .setPositiveButton("OK",new OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        // Some stuff to do when ok got clicked         
                        Log.e("bla","blablaa");


                        Intent intent = new Intent().setClass(BrowseServerActivity.this, Newactivity.class);
                        startActivity(intent);

                    }
                }).show();
            }
        }

        @Override
        public String toString()
        {
            return i.getTitle();
        }

        @Override 
        public boolean equals(Object o)
        {
            if(this == o)
                return true;
            if(getClass() != o.getClass())
                return false;
            return i.equals(((ServerContentItem)o).i);
        }
    }

我希望代码清楚。 THX

答案 1 :(得分:-1)

Cling Ressource类包含 URI 字段,获取并尝试。
Res CLING Support 1.0.5 API