android 怎么播放rtsp流
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 怎么播放rtsp流相关的知识,希望对你有一定的参考价值。
参考技术A package com.video.rtsp;import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.VideoView;
public class rtspActivity extends Activity
/** Called when the activity is first created. */
Button playButton ;
VideoView videoView ;
EditText rtspUrl ;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rtspUrl = (EditText)this.findViewById(R.id.url);
playButton = (Button)this.findViewById(R.id.start_play);
playButton.setOnClickListener(new Button.OnClickListener()
public void onClick(View v)
PlayRtspStream(rtspUrl.getEditableText().toString());
);
videoView = (VideoView)this.findViewById(R.id.rtsp_player);
//play rtsp stream
private void PlayRtspStream(String rtspUrl)
videoView.setVideoURI(Uri.parse(rtspUrl));
videoView.requestFocus();
videoView.start();
在 VideoView 中播放流,将 url 转换为 rtsp
【中文标题】在 VideoView 中播放流,将 url 转换为 rtsp【英文标题】:play streaming in VideoView, convert url to rtsp 【发布时间】:2013-04-14 22:15:44 【问题描述】:我需要以相同的布局播放 youtube 视频和录制视频。
要执行此操作,我搜索了 youtube api,发现 android 版本需要高于 2.2,这可以,但是我想使用 VideoView。
我在这里看到了一些关于此问题的帖子,并最终决定使用此代码在 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 的代码,我不能手动执行此操作,我检查了一些代码,但它们都不起作用...
我试试这个:从这里 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;
我使用上面的方法是这样的:
getUrlVideoRTSP(current_url)
当要测试的 currnt_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 ;
“videoView.setVideoURI(Uri.parse(rstp_url));”行中的代码崩溃,因为空异常 我查了一下,发现代码变量是 =tFXS9krT2VY ,好的.. 但 rstp_url 变量为 null
UdayKiran 给类似的 Q 写了这个答案,有人可以解释他的意思吗? 我不明白他的回答
他的回答:
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
这些正在 VideoView 中播放。
最后我不使用此代码和视频视图进行蒸汽
我使用 youtube android api,它是从 android 2.2 开始工作,而不是像我一样从 4.2 在我的 Q 中写道,这是伪装。
使用rtsp的结果视频质量很差,需要处理宽高比。
【问题讨论】:
我有一个应用程序,可以在视频视图中播放 youtube。但不幸的是,您的问题并不清楚。有什么问题?你想要什么? 抱歉,如果不清楚:我需要接收 youtube url 的代码:current_url = "m.youtube.com/#/watch?v=FlsBObg-1BQ" 并像这样返回 rtsp:String exemple = "rtsp://v4.cache3.c .youtube.com/CjYLENy73wIaLQlW_ji2apr6AxMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYOr_86Xm06e5UAw=/0/0/0/video.3gp"; 我找到了这个code.google.com/p/android-youtube-player/downloads/list,但是有些视频没有用这个打开,我需要使用视频视图所以不要使用它 好的,看看我的答案! 【参考方案1】:以下对我有用: 在您的情况下,代码 = 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];
getJsonString() 方法。
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();
不要因为网络请求而忘记将其包装在异步任务中。
【讨论】:
好的,让我知道结果。从 Froyo 到果冻豆,它对我有用 Q-GLuydiMe4 id不会通过这种方式打开?我试过但无法转换为 rtsp 链接,但有些工作正常。该视频 id 在 youtube 上完美运行。以上是关于android 怎么播放rtsp流的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 中播放 RTSP 流视频时如何保持宽高比?