play streaming in VideoView, convert url to rtsp

后端 未结 1 2008
太阳男子
太阳男子 2020-12-04 02:40

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

To perform this I search for youtube api and found that android version need to be higher than 2.2

相关标签:
1条回答
  • 2020-12-04 03:18

    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() 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.

    0 讨论(0)
提交回复
热议问题