package com.example.videodemo; 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.app.Activity; import android.app.ProgressDialog; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.MediaController; import android.widget.ProgressBar; import android.widget.Toast; import android.widget.VideoView; public class MainActivity extends Activity { private VideoView videoViewer; private ProgressBar mProgressBar; String videoUrl ="http://podcast.20min-tv.ch/podcast/20min/199733.mp4"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoViewer=(VideoView)findViewById(R.id.videoView1); //videoViewer.setVideoPath("/sdcard/documentariesandyou.mp4"); /* Uri uri=Uri.parse(path1); http://www.youtube.com/watch?v=Hxy8BZGQ5Jo videoViewer.setVideoURI(uri); */ mProgressBar = (ProgressBar) findViewById(R.id.Progressbar); mProgressBar.setProgress(0); mProgressBar.setMax(100); mProgressBar.setVisibility(View.INVISIBLE); videoViewer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Toast.makeText(MainActivity.this, "End Video", Toast.LENGTH_LONG).show(); } }); videoViewer.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer arg0, int arg1, int arg2) { return true; } }); new YourAsyncTask().execute(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Toast.makeText(MainActivity.this, "Pause Video", Toast.LENGTH_LONG).show(); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Toast.makeText(MainActivity.this, "Stop Video", Toast.LENGTH_LONG).show(); } @Override protected void onDestroy() { super.onDestroy(); //Toast.makeText(MainActivity.this, "Destroy Video", Toast.LENGTH_LONG).show(); /*WifiManager wifiManager = null; wifiManager.disconnect();*/ } private class YourAsyncTask extends AsyncTask<Void, Void, Void> { ProgressDialog progressDialog; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading Video wait...", true); } @Override protected Void doInBackground(Void... params) { try { String url = "https://www.youtube.com/watch?v=SyrO83x7g-E"; videoUrl = getUrlVideoRTSP(url); Log.e("Video url for playing=========>>>>>", videoUrl); } catch (Exception e) { Log.e("Login Soap Calling in Exception", e.toString()); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); try { progressDialog.dismiss(); videoViewer.setVideoURI(Uri.parse(videoUrl)); MediaController mc = new MediaController(MainActivity.this); //videoViewer.setMediaController(mc); videoViewer.requestFocus(); videoViewer.start(); mc.show(); } catch (Exception e) { e.printStackTrace(); } } } 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; } }
Saturday, 24 August 2013
play video from youtube
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment