play streaming in VideoView, convert url to rtsp

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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, this ok but, i want to use VideoView.

I saw some post here about this issue and decide eventually to use this code to 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 

this code works, but just with rtsp link like this :

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

I have multiple link in url therefore I need code to convert url to RTSP, I can't do this manually, I check some code and all of them do not work...

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  maps = new HashMap();                 for (int j = 0; j >", 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 use above method like this :

getUrlVideoRTSP(current_url) 

when currnt_url to test is :

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

I try this code and it does not work

private  class Syncyoutube extends AsyncTask {  @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

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 wrote this answer to similar Q , someone could explain what he means? i dont understand his answer

his answer :

Element rsp = (Element)entry.getElementsByTagName("media:content").item(1);                                String anotherurl=rsp.getAttribute("url"); 

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

These are playing in VideoView.


finally i dont use this code and videoview for steaming

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.

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

回答1:

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

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!