发挥vidoview流,转换网址RTSP [英] play streaming in vidoview, convert url to rtsp

查看:413
本文介绍了发挥vidoview流,转换网址RTSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在相同的布局播放YouTube视频和录制视频。

I need to play youtube video and record video in the same layout.

要执行此我搜索的YouTube API和发现,Android版本必须高于2.2,这一确定,但,我wnat使用视频查看

To perform this i search for youtube api and find that android version need to be higher than 2.2 , this ok but, i wnat to using video view

我看到一些张贴在这里对这个问题,并决定eventialy使用此$ C $下观看视频videoview。

I saw some post here about this issue and decide eventialy to use this code for watch video in videoview.

videoView = (VideoView) findViewById(R.id.your_video_view);

Log.d(TAG,getUrlVideoRTSP(current_url) + "  id yotube1  " );


//here type the url...
videoView.setVideoURI(Uri.parse(getUrlVideoRTSP(current_url)));



videoView.setMediaController(new MediaController(this)); //sets MediaController in the video view

videoView.requestFocus();//give focus to a specific view 
videoView.start();//starts the video

这code的工作,但仅仅是 RTSP 链接 像这样的:

this code work but just with rtsp link like this :

String exemple = "rtsp://v4.cache3.c.youtube.com/CjYLENy73wIaLQlW_ji2apr6AxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOr_86Xm06e5UAw=/0/0/0/video.3gp";

我在网址中的多个链接为此,我需要code的转换网址RTSP,我不能这样做manualy,我CHACK约code和所有的艾德里安不工作......

i have multiple link in url therefor i need code that convert url to RTSP,i cant do this manualy, i chack some code and all of tham do not work...

我尝试这个办法:从这里开始 如何获得RTSP URL?

i try this: from here How to get RTSP URL?

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;
}

我用上面这样的方法:

i using above method like this :

getUrlVideoRTSP(current_url)

当currnt_url测试是:

when currnt_url to test is :

current_url = "http://m.youtube.com/#/watch?v=FlsBObg-1BQ"

感谢...

我尝试这个code,它是行不通的。

i try this code and it's doesnt work

private  class Syncyoutube extends AsyncTask <Void , Void , Void>{

@Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        /**
        videoView.setMediaController(new MediaController(this)); //sets MediaController in the video view
        //   MediaController containing controls for a MediaPlayer                                  
        videoView.requestFocus();//give focus to a specific view 
        videoView.start();//starts the video
        */
    }


public String getRstpLinks(String code){
    String[] urls = new String[3];
    String link = "http://gdata.youtube.com/feeds/api/videos/" + code + "?alt=json";
    String json = getJsonString(link); // here you request from the server
    try {
        JSONObject obj = new JSONObject(json);
        String entry = obj.getString("entry");
        JSONObject enObj = new JSONObject(entry);
        String group = enObj.getString("media$group");
        JSONObject grObj = new JSONObject(group);
        String content = grObj.getString("media$content");
        JSONObject cntObj = new JSONObject(group);
        JSONArray array = grObj.getJSONArray("media$content");
        for(int j=0; j<array.length(); j++){
            JSONObject thumbs = array.getJSONObject(j);
            String url = thumbs.getString("url");
            urls[j] = url;
            Log.d(TAG, url);
            //data.setThumbUrl(thumbUrl);
        }


        Log.v(TAG, content);
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        urls[0] = urls[1] = urls[2] = null;
    }
    return urls[2];

}


public String getJsonString(String url){
    Log.e("Request URL", url);
    StringBuilder buffer = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet     request = new HttpGet( url );
    HttpEntity entity = null;
    try {
        HttpResponse response = client.execute(request);

        if( response.getStatusLine().getStatusCode() == 200 ){
            entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while( (line = br.readLine() )!= null ){
                buffer.append(line);
            }
            br.close();

        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        try {
            entity.consumeContent();
        } catch (Exception e) {
            Log.e(TAG, "Exception = " + e.toString() );
        }
    }

    return buffer.toString();
}





@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub


    code = id_current_url(current_url);
    //here type the url...
    String rstp_url = getRstpLinks(code);


    videoView.setVideoURI(Uri.parse(rstp_url));

     // the code crech in this line because null exeption
     // i chack this and discover that code variable is =tFXS9krT2VY , ok..
     // but rstp_url variable in null 

    Log.d(TAG,getRstpLinks(code) + "   idan id yotube1  " );
    return null;
}


}





public String id_current_url (String url) {

    String c_id = null ;

     c_id = url.substring((url.lastIndexOf("=")), url.length());

    return c_id ;
}   













}

在code崩溃videoView.setVideoURI(Uri.parse(rstp_url));行,因为空exeption 我CHACK这一点,并发现,code变量= tFXS9krT2VY,确定.. 但在空rstp_url变量

the code crash in " videoView.setVideoURI(Uri.parse(rstp_url)); " line because null exeption i chack this and discover that code variable is =tFXS9krT2VY , ok.. but rstp_url variable in null

UdayKiran写了这个答案类似Q,有人可以解释他是什么意思? 我不明白他的答案

UdayKiran wrote this answer to similar Q , someone could explain what he means? i dont understand his answer

他的回答:

Element rsp = (Element)entry.getElementsByTagName("media:content").item(1);

                              String anotherurl=rsp.getAttribute("url");

在GDATA API只有我们得到这种类型的链接:RTSP://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

In gdata api only we are getting this type of links : rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

这些正在发挥VideoView。

These are playing in VideoView.

终于我不使用这个code和videoview蒸

finally i dont use this code and videoview for steaming

我使用YouTube的Andr​​oid API,它是从Android 2.2的工作不是从4.2像我 写在我的Q,这是mastake。

i use youtube android api , it's work from android 2.2 not from 4.2 like i wrote in my Q , it was mastake.

使用的结果,RTSP它的视频质量差,需要处理的纵横比。

the result of using rtsp it's poor video quality and need to deal with aspect ratio.

推荐答案

下面的工作对我来说:
code = FlsBObg-1BQ你的情况。
你会得到很多的网址,我选择回到最佳的质量。

The following works for me:
code = FlsBObg-1BQ in your case.
You will get lots of urls, and I choose to return the best quality.

private String getRstpLinks(String code){
    String[] urls = new String[3];
    String link = "http://gdata.youtube.com/feeds/api/videos/" + code + "?alt=json";
    String json = getJsonString(link); // here you request from the server
    try {
        JSONObject obj = new JSONObject(json);
        String entry = obj.getString("entry");
        JSONObject enObj = new JSONObject(entry);
        String group = enObj.getString("media$group");
        JSONObject grObj = new JSONObject(group);
        String content = grObj.getString("media$content");
        JSONObject cntObj = new JSONObject(group);
        JSONArray array = grObj.getJSONArray("media$content");
        for(int j=0; j<array.length(); j++){
            JSONObject thumbs = array.getJSONObject(j);
            String url = thumbs.getString("url");
            urls[j] = url;
            Log.d(TAG, url);
            //data.setThumbUrl(thumbUrl);
        }


        Log.v(TAG, content);
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        urls[0] = urls[1] = urls[2] = null;
    }
    return urls[2];

}

getJsonString()方法。

getJsonString() method.

public static String getJsonString(String url){
    Log.e("Request URL", url);
    StringBuilder buffer = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet     request = new HttpGet( url );
    HttpEntity entity = null;
    try {
        HttpResponse response = client.execute(request);

        if( response.getStatusLine().getStatusCode() == 200 ){
            entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while( (line = br.readLine() )!= null ){
                buffer.append(line);
            }
            br.close();

        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        try {
            entity.consumeContent();
        } catch (Exception e) {
            Log.e(TAG, "Exception = " + e.toString() );
        }
    }

    return buffer.toString();
}

不要忘了包,因为网络请求它的内部异步任务。

Don't forget to wrap it inside async task because of a network request.

这篇关于发挥vidoview流,转换网址RTSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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