0

//这个方法只是显示youtube加载

  1. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + videoId)) startActivity(intent);

//第二种方法根本不播放视频 2 .

public class MyTask extends AsyncTask<Void, Void, Void>
    {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        mProgressBar.setVisibility(LinearLayout.VISIBLE);

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        url=getUrlVideoRTSP(urlRstp[0]);
        rstlUrl=Uri.parse(url);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //Toast.makeText(VODVideoPlayActivity.this, url, Toast.LENGTH_LONG).show();
        //System.out.println(url);

        mVideoView.setVideoURI(rstlUrl);
        //MediaController mc = new MediaController(VODVideoPlayActivity.this);
        //mVideoView.setMediaController(mc);
        //mVideoView.requestFocus();
        mVideoView.start();          
        //mc.show();
        mProgressBar.setVisibility(LinearLayout.GONE);          
    }




}

public static String getUrlVideoRTSP(String urlYoutube)
{
    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();
        Document doc = documentBuilder.parse(connection.getInputStream());
        Element el = doc.getDocumentElement();
        NodeList list = 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 static String extractYoutubeId(String url) throws MalformedURLException
4

1 回答 1

0

调用新的 VideoAsyncTask().execute();

private class VideoAsyncTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog progressDialog;
    String videoUrl;

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

    @Override
    protected Void doInBackground(Void... params) {
        try {
            //

            String id = ""; //video id of youtube

            String url = "http://www.youtube.com/watch?v=" + id;
            videoUrl = getUrlVideoRTSP(url);
            Log.e("Video url for playing=========>>>>>", videoUrl);
        } catch (Exception e) {
            Log.e("Login Soap Calling in Exception", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
        /*
         * videoView.setVideoURI(Uri.parse(
         * "rtsp://v4.cache1.c.youtube.com/CiILENy73wIaGQk4RDShYkdS1BMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
         * )); videoView.setMediaController(new
         * MediaController(AlertDetail.this)); videoView.requestFocus();
         * videoView.start();
         */

        if (myvideoview.isPlaying())
            myvideoview.stopPlayback();
        myvideoview.setVideoURI(Uri.parse(videoUrl));
        MediaController mc = new MediaController(GallaryActivity.this);
        myvideoview.setMediaController(mc);
        myvideoview.requestFocus();

        myvideoview.start();
        mc.show();
    }

}

public static String getUrlVideoRTSP(String urlYoutube) {
    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();
        Document doc = documentBuilder.parse(connection.getInputStream());
        Element el = doc.getDocumentElement();
        NodeList list = 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 static 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;
}
于 2013-07-09T05:51:42.437 回答