Android 在VideoView中播放流媒体,将url转换为rtsp

Android 在VideoView中播放流媒体,将url转换为rtsp,android,youtube,streaming,android-videoview,rtsp,Android,Youtube,Streaming,Android Videoview,Rtsp,我需要播放youtube视频并在同一版面中录制视频 要执行此操作,我搜索youtube api,发现android版本需要高于2.2,这没问题,但我想使用VideoView videoView = (VideoView) findViewById(R.id.your_video_view); Log.d(TAG,getUrlVideoRTSP(current_url) + " id yotube1 " ); //here type the url... videoView.setVid

我需要播放youtube视频并在同一版面中录制视频

要执行此操作,我搜索youtube api,发现android版本需要高于2.2,这没问题,但我想使用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
我在这里看到了一些关于这个问题的帖子,并最终决定使用这个代码在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
此代码可以工作,但只需使用rtsp链接,如下所示:

String exemple = "rtsp://v4.cache3.c.youtube.com/CjYLENy73wIaLQlW_ji2apr6AxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOr_86Xm06e5UAw=/0/0/0/video.3gp";
我在url中有多个链接,因此我需要代码将url转换为RTSP,我无法手动执行此操作,我检查了一些代码,但它们都不工作

我试试这个:从这里开始

当要测试的当前url为:

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

我尝试了这个代码,但它不起作用

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













}
仅在gdata api中,我们获得了此类链接:rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

这些正在视频视图中播放

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

最后,我不使用此代码和视频视图进行蒸汽处理

我使用的是youtube android api,它是从android 2.2开始的,而不是像我一样从4.2开始的 在我的Q中写道,这是mastake


使用rtsp的结果是视频质量很差,需要处理纵横比。

以下内容适合我:
在您的情况下,代码=FlsBObg-1BQ
你会得到很多网址,我选择返回最好的质量

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

}

由于网络请求,不要忘记将其包装在异步任务中。

我有一个应用程序,可以在视频视图中播放youtube。但不幸的是,你的问题并不清楚。有什么问题吗?你想要什么?如果不清楚,很抱歉:我需要这样的代码来接收youtube url:current_url=“”,并像这样返回rtsp:String Example=”rtsp://v4.cache3.c.youtube.com/CjYLENy73wIaLQlW_ji2apr6AxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOr_86Xm06e5UAw=/0/0/0/video.3gp";我找到了这个,但是一些视频没有打开,我需要使用视频视图,所以不要使用itokay,检查我的答案!好的,让我知道结果。从Froyo到jeally beansQ-GLuydiMe4的id不会通过这种方式打开,但对我来说是有效的?我尝试了,但不能转换成rtsp链接,但有些工作正常。视频id在youtube上运行得非常完美。
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];

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