public class YoutubeActivity extends AppCompatActivity {
private VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube);
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 MediaPlayer.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;
}
}
}
你可以给我一个解决方案。
错误记录
E/MediaPlayer: setDataSource: IOException! uri=http://www.youtube.com/watch?v=2zNSgSzhBfM
java.io.FileNotFoundException: No content provider: http://www.youtube.com/watch?v=2zNSgSzhBfM
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1123)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:958)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:881)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1018)
at android.widget.VideoView.openVideo(VideoView.java:437)
at android.widget.VideoView.setVideoURI(VideoView.java:342)
at android.widget.VideoView.setVideoURI(VideoView.java:325)
at com.ulguru.lms.testyoutube.YoutubeActivity.startPlaying(YoutubeActivity.java:68)
at com.ulguru.lms.testyoutube.YoutubeActivity$RTSPUrlTask.onPostExecute(YoutubeActivity.java:95)
at com.ulguru.lms.testyoutube.YoutubeActivity$RTSPUrlTask.onPostExecute(YoutubeActivity.java:85)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
D/MediaPlayer: Couldn't open file on client side, trying server side