Android : how to play youtube videos in video view

on Thursday, January 8, 2015


i am developing to play youtube videos in videoview, i am getting error like "Can't play this video". i used this code to play youtube videos in video view



public class MainActivity extends Activity {
VideoView videoView;
String videoUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
videoView = (VideoView)findViewById(R.id.videoView);


new YourAsyncTask().execute();
}

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 = "http://www.youtube.com/watch?v=OtLa7wDpuOU";
videoUrl = getUrlVideoRTSP(url);
//Toast.makeText(getApplicationContext(), videoUrl, Toast.LENGTH_LONG).show();

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);
progressDialog.dismiss();

videoView.setVideoURI(Uri.parse(videoUrl));
MediaController mc = new MediaController(MainActivity.this);
videoView.setMediaController(mc);
videoView.requestFocus();
videoView.start();
mc.show();
}

}

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;
}
public void onPause ()
{
super.onPause();
videoView.stopPlayback();
}
}


this is my logcat error, i am getting url error and also couldn't open file on client side trying server side



01-09 10:37:24.220: E/MediaPlayer(11571): stop called in state 4
01-09 10:37:24.221: E/MediaPlayer(11571): error (-38, 0)
01-09 10:37:24.243: W/MediaPlayer(11571): mediaplayer went away with unhandled events
01-09 10:37:24.255: I/InputMethodManager(11571): handleMessage: MSG_SET_ACTIVE false, was true
01-09 10:37:24.257: V/InputMethodManager(11571): START INPUT: android.widget.VideoView{4126c2e8 VFE..... .F...... 0,0-800,480 #7f080000 app:id/videoView} ic=null tba=android.view.inputmethod.EditorInfo@412ceb18 controlFlags=#100
01-09 10:37:24.259: V/InputMethodManager(11571): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@412cf458 com.android.inputmethod.latin/.LatinIME #184}
01-09 10:37:29.931: D/OpenGLRenderer(11571): Flushing caches (mode 0)
01-09 10:37:29.938: W/IInputConnectionWrapper(11571): beginBatchEdit on inactive InputConnection
01-09 10:37:29.939: W/IInputConnectionWrapper(11571): endBatchEdit on inactive InputConnection
01-09 10:37:29.952: I/SurfaceView(11571): Changes: creating=false format=false size=false visible=true left=false top=false mUpdateWindowNeeded=false mReportDrawNeeded=false redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=false
01-09 10:37:29.954: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=581)
01-09 10:37:30.045: I/SurfaceView(11571): New surface: Surface(name=null, identity=-1), vis=false, frame=Rect(0, 0 - 800, 480)
01-09 10:37:30.052: I/SurfaceView(11571): visibleChanged -- surfaceDestroyed
01-09 10:37:30.258: V/SurfaceView(11571): Layout: x=0 y=0 w=800 h=480, frame=Rect(0, 0 - 800, 480)
01-09 10:37:30.267: D/OpenGLRenderer(11571): Flushing caches (mode 0)
01-09 10:37:30.494: V/PhoneWindow(11571): DecorView setVisiblity: visibility = 4
01-09 10:37:30.498: V/PhoneWindow(11571): DecorView setVisiblity: visibility = 0
01-09 10:37:30.499: W/IInputConnectionWrapper(11571): showStatusIcon on inactive InputConnection
01-09 10:37:30.574: V/InputMethodManager(11571): Not IME target window, ignoring
01-09 10:37:30.605: I/SurfaceView(11571): Changes: creating=true format=true size=true visible=true left=true top=true mUpdateWindowNeeded=false mReportDrawNeeded=false redrawNeeded=false forceSizeChanged=true mVisible=false mRequestedVisible=true
01-09 10:37:30.611: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=-1)
01-09 10:37:30.620: V/SurfaceView(11571): android.widget.VideoView{412d48a8 VFE..... .F....ID 0,0-480,800 #7f080000 app:id/videoView} got resized: w=480 h=800, cur w=-1 h=-1
01-09 10:37:30.625: I/SurfaceView(11571): New surface: Surface(name=null, identity=591), vis=true, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.629: I/SurfaceView(11571): visibleChanged -- surfaceCreated
01-09 10:37:30.629: I/SurfaceView(11571): surfaceChanged -- format=4 w=480 h=800
01-09 10:37:30.629: I/SurfaceView(11571): surfaceRedrawNeeded
01-09 10:37:30.629: I/SurfaceView(11571): finishedDrawing
01-09 10:37:30.635: V/SurfaceView(11571): Layout: x=0 y=0 w=480 h=800, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.715: I/SurfaceView(11571): Changes: creating=false format=false size=false visible=false left=false top=false mUpdateWindowNeeded=true mReportDrawNeeded=true redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=true
01-09 10:37:30.715: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=591)
01-09 10:37:30.721: I/SurfaceView(11571): New surface: Surface(name=null, identity=591), vis=true, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.721: I/SurfaceView(11571): surfaceRedrawNeeded
01-09 10:37:30.721: I/SurfaceView(11571): finishedDrawing
01-09 10:37:30.722: V/SurfaceView(11571): Layout: x=0 y=0 w=480 h=800, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.739: I/SurfaceView(11571): Changes: creating=false format=false size=false visible=false left=false top=false mUpdateWindowNeeded=false mReportDrawNeeded=true redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=true
01-09 10:37:30.740: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=591)
01-09 10:37:30.744: I/SurfaceView(11571): New surface: Surface(name=null, identity=591), vis=true, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.747: I/SurfaceView(11571): surfaceRedrawNeeded
01-09 10:37:30.747: I/SurfaceView(11571): finishedDrawing
01-09 10:37:30.751: V/SurfaceView(11571): Layout: x=0 y=0 w=480 h=800, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.815: I/SurfaceView(11571): Changes: creating=false format=false size=false visible=false left=false top=false mUpdateWindowNeeded=false mReportDrawNeeded=true redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=true
01-09 10:37:30.816: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=591)
01-09 10:37:30.822: I/SurfaceView(11571): New surface: Surface(name=null, identity=591), vis=true, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.825: I/SurfaceView(11571): surfaceRedrawNeeded
01-09 10:37:30.825: I/SurfaceView(11571): finishedDrawing
01-09 10:37:30.828: V/SurfaceView(11571): Layout: x=0 y=0 w=480 h=800, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.833: I/SurfaceView(11571): Changes: creating=false format=false size=false visible=false left=false top=false mUpdateWindowNeeded=false mReportDrawNeeded=true redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=true
01-09 10:37:30.833: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=591)
01-09 10:37:30.842: I/SurfaceView(11571): New surface: Surface(name=null, identity=591), vis=true, frame=Rect(0, 0 - 480, 800)
01-09 10:37:30.842: I/SurfaceView(11571): surfaceRedrawNeeded
01-09 10:37:30.842: I/SurfaceView(11571): finishedDrawing
01-09 10:37:30.844: V/SurfaceView(11571): Layout: x=0 y=0 w=480 h=800, frame=Rect(0, 0 - 480, 800)
01-09 10:37:31.169: I/System.out(11571): [CDS]rx timeout:0
01-09 10:37:31.581: E/Video url for playing=========>>>>>(11571): rtsp://r4---sn-a5m7zu7d.c.youtube.com/CiILENy73wIaGQnluOkA79rSOhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
01-09 10:37:31.581: D/OpenGLRenderer(11571): Flushing caches (mode 0)
01-09 10:37:31.598: E/MediaPlayer(11571): setDataSource: IOException! uri=rtsp://r4---sn-a5m7zu7d.c.youtube.com/CiILENy73wIaGQnluOkA79rSOhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
01-09 10:37:31.598: D/MediaPlayer(11571): Couldn't open file on client side, trying server side
01-09 10:37:31.598: D/MediatekClassFactory(11571): createInstance(): Begin = 7542179
01-09 10:37:31.598: D/MediatekClassFactory(11571): create Instance with : interface com.mediatek.common.media.IOmaSettingHelper
01-09 10:37:31.599: W/MediatekClassFactory(11571): OP not exist!, Get obj from default class
01-09 10:37:31.599: D/MediatekClassFactory(11571): create Instance from operator library : com.mediatek.op.media.DefaultOmaSettingHelper
01-09 10:37:31.599: D/MediatekClassFactory(11571): createInstance(): End = 7542181
01-09 10:37:31.599: V/DefaultOmaSettingHelper(11571): isOMAEnabled: enabled=true
01-09 10:37:31.600: V/DefaultOmaSettingHelper(11571): judgeStreamingType(rtsp://r4---sn-a5m7zu7d.c.youtube.com/CiILENy73wIaGQnluOkA79rSOhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp)
01-09 10:37:31.600: V/DefaultOmaSettingHelper(11571): judgeStreamingType: type=2
01-09 10:37:31.600: I/DefaultOmaSettingHelper(11571): setOmaSettingHeader(com.example.videoview.MainActivity@412d1990,null)
01-09 10:37:31.600: V/Provider/Settings(11571): invalidate [system]: current 209 != cached 207
01-09 10:37:31.604: V/Provider/Settings(11571): from db cache, name = MTK-HTTP-CACHE-SIZE , value = null
01-09 10:37:31.608: V/Provider/Settings(11571): from db cache, name = MTK-RTSP-CACHE-SIZE , value = null
01-09 10:37:31.611: V/Provider/Settings(11571): from db cache, name = mtk_rtsp_min_udp_port , value = 1024
01-09 10:37:31.614: V/Provider/Settings(11571): from db cache, name = mtk_rtsp_max_udp_port , value = 65535
01-09 10:37:31.617: V/Provider/Settings(11571): from db cache, name = mtk_rtsp_proxy_enabled , value = null
01-09 10:37:31.621: V/Provider/Settings(11571): from db cache, name = mtk_http_proxy_enabled , value = null
01-09 10:37:31.622: V/DefaultOmaSettingHelper(11571): setOmaSettingHeader: params:minUdpPort=1024minUdpPort=1024maxUdpPort=65535rtspProxyEnable=0rtspProxyHost=nullrtspProxyPort=-1httpProxyEnable=0httpProxyHost=nullhttpProxyPort=-1httpBufferSize=10rtspBufferSize=4
01-09 10:37:31.627: D/MediaPlayer(11571): Don't notify duration to com.example.videoview!
01-09 10:37:31.679: I/SurfaceView(11571): Changes: creating=false format=false size=false visible=false left=false top=false mUpdateWindowNeeded=false mReportDrawNeeded=true redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=true
01-09 10:37:31.679: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=591)
01-09 10:37:31.684: I/SurfaceView(11571): New surface: Surface(name=null, identity=591), vis=true, frame=Rect(0, 0 - 480, 800)
01-09 10:37:31.685: I/SurfaceView(11571): surfaceRedrawNeeded
01-09 10:37:31.685: I/SurfaceView(11571): finishedDrawing
01-09 10:37:31.688: V/SurfaceView(11571): Layout: x=0 y=0 w=480 h=800, frame=Rect(0, 0 - 480, 800)
01-09 10:37:31.689: I/SurfaceView(11571): Changes: creating=false format=false size=false visible=false left=false top=false mUpdateWindowNeeded=false mReportDrawNeeded=true redrawNeeded=false forceSizeChanged=false mVisible=true mRequestedVisible=true
01-09 10:37:31.690: I/SurfaceView(11571): Cur surface: Surface(name=null, identity=591)
01-09 10:37:31.704: I/SurfaceView(11571): New surface: Surface(name=null, identity=591), vis=true, frame=Rect(0, 0 - 480, 800)
01-09 10:37:31.704: I/SurfaceView(11571): surfaceRedrawNeeded
01-09 10:37:31.704: I/SurfaceView(11571): finishedDrawing
01-09 10:37:31.706: V/SurfaceView(11571): Layout: x=0 y=0 w=480 h=800, frame=Rect(0, 0 - 480, 800)
01-09 10:37:31.714: V/InputMethodManager(11571): Not IME target window, ignoring
01-09 10:37:34.664: D/OpenGLRenderer(11571): Flushing caches (mode 0)
01-09 10:38:31.653: E/MediaPlayer(11571): error (261, -1003)
01-09 10:38:31.654: E/MediaPlayer(11571): Error (261,-1003)
01-09 10:38:31.655: D/VideoView(11571): Error: 261,-1003


Please suggest me a way to load youtube videos in android video view.


Thanks in Advance...


0 comments:

Post a Comment