来自 YouTube 的视频源文件使 Android Studio 中的应用程序崩溃
Posted
技术标签:
【中文标题】来自 YouTube 的视频源文件使 Android Studio 中的应用程序崩溃【英文标题】:Video Source file from YouTube crashes app in Android Studio 【发布时间】:2016-03-27 19:15:15 【问题描述】:当视频位于带有媒体控制器的原始文件夹中时,应用程序可以正常工作。但是,当我将 Youtube 视频中的嵌入 src 值放入 setVideoPath 括号时,应用程序崩溃了。请帮忙!
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.VideoView;
import android.widget.MediaController;
公共类 MainActivity 扩展 AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView vid = (VideoView) findViewById(R.id.videoView);
vid.setVideoPath("https://www.youtube.com/embed/8iPFYfXuygk");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vid);
vid.setMediaController(mediaController);
vid.start();
【问题讨论】:
你在模拟器上测试吗? 你为什么不试试developers.google.com/youtube/android/player 如果您想播放 youtube 视频,那么我认为您必须在 youtube 上引用直接 mp4 链接,否则默认视频无法播放上述短网址 @BilalShahid:不,我正在使用手机进行测试。是的,我认为这更有意义。非常感谢:) 【参考方案1】:此Example 代码适用于 youtube 使用 videoview 播放视频
package com.truiton.videoview;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoViewActivity extends Activity
private VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
videoView = (VideoView) findViewById(R.id.videoView1);
mController = new MediaController(this);
videoView.setMediaController(mController);
videoView.requestFocus();
/*Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/"
+ R.raw.sample);
videoView.setVideoURI(uri);
videoView.start();*/
if (savedInstanceState != null)
int loc = savedInstanceState.getInt("Loc");
Log.i("Loaction: ", loc + "");
uriYouTube = Uri.parse(savedInstanceState.getString("url"));
videoView.setVideoURI(uriYouTube);
videoView.seekTo(loc);
videoView.setOnPreparedListener(new OnPreparedListener()
@Override
public void onPrepared(MediaPlayer mp)
Log.v("onPrepared", "ok");
mp.start();
);
else
RTSPUrlTask truitonTask = new RTSPUrlTask();
truitonTask.execute("http://www.youtube.com/watch?v=2zNSgSzhBfM");
void startPlaying(String url)
uriYouTube = Uri.parse(url);
videoView.setVideoURI(uriYouTube);
videoView.start();
@Override
protected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
outState.putInt("Loc", videoView.getCurrentPosition());
outState.putString("url", uriYouTube.toString());
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.video_view, menu);
return true;
private class RTSPUrlTask extends AsyncTask<String, Void, String>
@Override
protected String doInBackground(String... urls)
String response = getRTSPVideoUrl(urls[0]);
return response;
@Override
protected void onPostExecute(String result)
startPlaying(result);
public String getRTSPVideoUrl(String urlYoutube)
try
String gdy = "http://gdata.youtube.com/feeds/api/videos/";
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
String id = extractYoutubeId(urlYoutube);
URL url = new URL(gdy + id);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
Document doc = dBuilder.parse(connection.getInputStream());
Element el = doc.getDocumentElement();
NodeList list = el.getElementsByTagName("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)
return urlYoutube;
public String extractYoutubeId(String url) throws MalformedURLException
String query = new URL(url).getQuery();
String[] param = query.split("&");
String id = null;
for (String row : param)
String[] param1 = row.split("=");
if (param1[0].equals("v"))
id = param1[1];
return id;
添加添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
【讨论】:
以上是关于来自 YouTube 的视频源文件使 Android Studio 中的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
如何使添加到 iframe 中的 youtube 视频可访问?