如何通过视频查看Android原生应用发挥你管的视频? [英] How to play You tube video in android native Application through video view?

查看:136
本文介绍了如何通过视频查看Android原生应用发挥你管的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在本土Android应用程序来播放YouTube。
这是我的code,但我得到了像无法播放此视频的结果,所以你可以请帮助什么是错在code

 公共类Youtubeplay扩展的AsyncTask<太虚,太虚,太虚> {
    ProgressDialog progressDialog;    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        progressDialog = ProgressDialog.show(MainActivity.this,,
                加载视频稍候...,真正的);
    }    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        尝试{
            字符串URL =htt​​ps://www.youtube.com/watch?v=ZA7Kkhv30WA;
            videoUrl = getUrlVideoRTSP(URL);
            Log.e(视频网址为播放=========>>>>>中,videoUrl);
        }赶上(例外五){
            Log.e(登录肥皂在调用异常,e.toString());
        }
        返回null;
    }    私人字符串getUrlVideoRTSP(字符串urlYoutube){
        // TODO自动生成方法存根
        尝试{
            字符串GDY =htt​​p://gdata.youtube.com/feeds/api/videos/;
            的DocumentBuilder的DocumentBuilder =的DocumentBuilderFactory
                    。.newInstance()newDocumentBuilder();
            字符串ID = extractYoutubeId(urlYoutube);
            网址URL =新的URL(GDY + ID);
            HttpURLConnection的连接=(HttpURLConnection类)网址
                    .openConnection();
            org.w3c.dom.Document中DOC = documentBuilder.parse(连接
                    .getInputStream());
            元件EL =(元)doc.getDocumentElement();
            节点列表清单=((org.w3c.dom.Document中)EL)
                    .getElementsByTagName(媒体:内容); // /媒体:内容
            字符串指针= urlYoutube;
            的for(int i = 0; I< list.getLength();我++){
                节点node = list.item(ⅰ);
                如果(节点!= NULL){
                    的NamedNodeMap nodeMap = node.getAttributes();
                    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                    对于(INT J = 0; J< nodeMap.getLength(); J ++){
                        ATTR ATT =(attr)使用nodeMap.item(J);
                        maps.put(att.getName(),att.getValue());
                    }
                    如果(maps.containsKey(YT:格式)){
                        串f = maps.get(YT:格式);
                        如果(maps.containsKey(URL)){
                            光标= maps.get(URL);
                        }
                        如果(f.equals(1))
                            返回游标;
                    }
                }
            }
            返回游标;
        }赶上(例外前){
            Log.e(获取网址视频RTSP异常======>>中,ex.toString());
        }
        返回urlYoutube;
    }    保护字符串extractYoutubeId(字符串URL)
            抛出MalformedURLException的{
        字符串ID = NULL;
        尝试{
            查询字符串=新的URL(网址).getQuery();
            如果(查询!= NULL){
                的String []参数= query.split(与&);
                对于(字符串行:参数){
                    串[]的param1 = row.split(=);
                    如果(参数1 [0] .equals(V)){
                        ID =参数1 [1];
                    }
                }
            }其他{
                如果(url.contains(嵌入)){
                    ID = url.substring(url.lastIndexOf(/)+ 1);
                }
            }
        }赶上(例外前){
            Log.e(异常,ex.toString());
        }
        返回ID;
    }    @覆盖
    保护无效onPostExecute(虚空结果){
        super.onPostExecute(结果);
        progressDialog.dismiss();        videoView.setVideoURI(Uri.parse(videoUrl));
        MC的MediaController =新的MediaController(MainActivity.this);
        videoView.setMediaController(MC);
        videoView.requestFocus();
        videoView.start();
        mc.show();
    }}


解决方案

称之为code在mainactivity.java

 如果(savedInstanceState!= NULL){
                INT LOC = savedInstanceState.getInt(禄);
                Log.i(位置:LOC +);
                uriYouTube = Uri.parse(savedInstanceState.getString(URL));
                videoView.setVideoURI(uriYouTube);
                videoView.seekTo(LOC);
                videoView.setOn preparedListener(新在preparedListener(){
                    @覆盖
                    在prepared(MediaPlayer的MP)公共无效{
                        Log.v(关于prepared,OK);
                        mp.start();
                    }
                });
            }其他{
                新RTSPUrlTask​​()执行(model.VideoUrl);
            }私有类RTSPUrlTask​​扩展的AsyncTask<弦乐,太虚,字符串> {
        @覆盖
        保护字符串doInBackground(字符串的URL ...){
            字符串的响应= getRTSPVideoUrl(网址[0]);
            返回响应;
        }        @覆盖
        保护无效onPostExecute(字符串结果){
            startPlaying(结果);
        }        公共字符串getRTSPVideoUrl(字符串urlYoutube){
            尝试{
                字符串GDY =htt​​p://gdata.youtube.com/feeds/api/videos/;
                的DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder();
                字符串ID = extractYoutubeId(urlYoutube);
                网址URL =新的URL(GDY + ID);
                HttpURLConnection的连接=(HttpURLConnection类)网址
                        .openConnection();
                文档的文档= dBuilder.parse(connection.getInputStream());
                元件EL = doc.getDocumentElement();
                节点列表清单= el.getElementsByTagName(媒体:内容);
                字符串指针= urlYoutube;
                的for(int i = 0; I< list.getLength();我++){
                    节点node = list.item(ⅰ);
                    如果(节点!= NULL){
                        的NamedNodeMap nodeMap = node.getAttributes();
                        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                        对于(INT J = 0; J< nodeMap.getLength(); J ++){
                            ATTR ATT =(attr)使用nodeMap.item(J);
                            maps.put(att.getName(),att.getValue());
                        }
                        如果(maps.containsKey(YT:格式)){
                            串f = maps.get(YT:格式);
                            如果(maps.containsKey(URL))
                                光标= maps.get(URL);
                            如果(f.equals(1))
                                返回游标;
                        }
                    }
                }
                返回游标;
            }赶上(例外前){
                返回urlYoutube;
            }
        }        公共字符串extractYoutubeId(字符串URL)抛出MalformedURLException的{
            查询字符串=新的URL(网址).getQuery();
            的String []参数= query.split(与&);
            字符串ID = NULL;
            对于(字符串行:参数){
                串[]的param1 = row.split(=);
                如果(参数1 [0] .equals(V)){
                    ID =参数1 [1];
                }
            }
            返回ID;
        }
    }    无效startPlaying(字符串URL){
        uriYouTube = Uri.parse(URL);
        videoView.setVideoURI(uriYouTube);
        videoView.start();
    }

我希望它有用you.in我的项目,其工作的罚款。

I am trying to play YouTube in Native Android App. This is My code But I got the result like Can't Play this video, So can you please help what is wrong in that code

public class Youtubeplay extends AsyncTask<Void, Void, Void> {
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(MainActivity.this, "",
                "Loading Video wait...", true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            String url = "https://www.youtube.com/watch?v=ZA7Kkhv30WA";
            videoUrl = getUrlVideoRTSP(url);
            Log.e("Video url for playing=========>>>>>", videoUrl);
        } catch (Exception e) {
            Log.e("Login Soap Calling in Exception", e.toString());
        }
        return null;
    }

    private String getUrlVideoRTSP(String urlYoutube) {
        // TODO Auto-generated method stub
        try {
            String gdy = "http://gdata.youtube.com/feeds/api/videos/";
            DocumentBuilder documentBuilder = DocumentBuilderFactory
                    .newInstance().newDocumentBuilder();
            String id = extractYoutubeId(urlYoutube);
            URL url = new URL(gdy + id);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            org.w3c.dom.Document doc = documentBuilder.parse(connection
                    .getInputStream());
            Element el = (Element) doc.getDocumentElement();
            NodeList list = ((org.w3c.dom.Document) el)
                    .getElementsByTagName("media:content");// /media:content
            String cursor = urlYoutube;
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                if (node != null) {
                    NamedNodeMap nodeMap = node.getAttributes();
                    HashMap<String, String> maps = new HashMap<String, String>();
                    for (int j = 0; j < nodeMap.getLength(); j++) {
                        Attr att = (Attr) nodeMap.item(j);
                        maps.put(att.getName(), att.getValue());
                    }
                    if (maps.containsKey("yt:format")) {
                        String f = maps.get("yt:format");
                        if (maps.containsKey("url")) {
                            cursor = maps.get("url");
                        }
                        if (f.equals("1"))
                            return cursor;
                    }
                }
            }
            return cursor;
        } catch (Exception ex) {
            Log.e("Get Url Video RTSP Exception======>>", ex.toString());
        }
        return urlYoutube;
    }

    protected String extractYoutubeId(String url)
            throws MalformedURLException {
        String id = null;
        try {
            String query = new URL(url).getQuery();
            if (query != null) {
                String[] param = query.split("&");
                for (String row : param) {
                    String[] param1 = row.split("=");
                    if (param1[0].equals("v")) {
                        id = param1[1];
                    }
                }
            } else {
                if (url.contains("embed")) {
                    id = url.substring(url.lastIndexOf("/") + 1);
                }
            }
        } catch (Exception ex) {
            Log.e("Exception", ex.toString());
        }
        return id;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDialog.dismiss();

        videoView.setVideoURI(Uri.parse(videoUrl));
        MediaController mc = new MediaController(MainActivity.this);
        videoView.setMediaController(mc);
        videoView.requestFocus();
        videoView.start();
        mc.show();
    }

}

解决方案

call this code in mainactivity.java

if (savedInstanceState != null) {
                int loc = savedInstanceState.getInt("Loc");
                Log.i("Loaction: ", loc + "");
                uriYouTube = Uri.parse(savedInstanceState.getString("url"));
                videoView.setVideoURI(uriYouTube);
                videoView.seekTo(loc);
                videoView.setOnPreparedListener(new OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        Log.v("onPrepared", "ok");
                        mp.start();
                    }
                });
            } else {
                new RTSPUrlTask().execute(model.VideoUrl);
            }



private class RTSPUrlTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = getRTSPVideoUrl(urls[0]);
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            startPlaying(result);
        }

        public String getRTSPVideoUrl(String urlYoutube) {
            try {
                String gdy = "http://gdata.youtube.com/feeds/api/videos/";
                DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder();
                String id = extractYoutubeId(urlYoutube);
                URL url = new URL(gdy + id);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                Document doc = dBuilder.parse(connection.getInputStream());
                Element el = doc.getDocumentElement();
                NodeList list = el.getElementsByTagName("media:content");
                String cursor = urlYoutube;
                for (int i = 0; i < list.getLength(); i++) {
                    Node node = list.item(i);
                    if (node != null) {
                        NamedNodeMap nodeMap = node.getAttributes();
                        HashMap<String, String> maps = new HashMap<String, String>();
                        for (int j = 0; j < nodeMap.getLength(); j++) {
                            Attr att = (Attr) nodeMap.item(j);
                            maps.put(att.getName(), att.getValue());
                        }
                        if (maps.containsKey("yt:format")) {
                            String f = maps.get("yt:format");
                            if (maps.containsKey("url"))
                                cursor = maps.get("url");
                            if (f.equals("1"))
                                return cursor;
                        }
                    }
                }
                return cursor;
            } catch (Exception ex) {
                return urlYoutube;
            }
        }

        public String extractYoutubeId(String url) throws MalformedURLException {
            String query = new URL(url).getQuery();
            String[] param = query.split("&");
            String id = null;
            for (String row : param) {
                String[] param1 = row.split("=");
                if (param1[0].equals("v")) {
                    id = param1[1];
                }
            }
            return id;
        }
    }

    void startPlaying(String url) {
        uriYouTube = Uri.parse(url);
        videoView.setVideoURI(uriYouTube);
        videoView.start();
    }

I hope its useful to you.in my project ,its working fine.

这篇关于如何通过视频查看Android原生应用发挥你管的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆