Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grab a frame from youtube video in android?

I am using videoview to show the following youtube video

http://www.youtube.com/watch?v=FovFG3N_RSU

I do not want to see the black screen but want to display midframe of the video as the cover of video .

In IOS , there is

https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVAssetImageGenerator_Class/Reference/Reference.html which helps .

How to accomplish in android ?

like image 446
Preethi Avatar asked Dec 05 '25 14:12

Preethi


1 Answers

It depends on device android version and the flash support feature. You cannot play video using VideoView setVideoUri method (after parsing http link as uri) unless you have flash installed on the device.

This below code is working for me but this is kind of a hack to you tube.
Use this code for devices which dont have flash installed.

 public Uri getYoutubeUriForVideoId()
 {
        String YOUTUBE_VIDEO_INFORMATION_URL = "http://m.youtube.com/watch?ajax=1&layout=mobile&tsp=1&utcoffset=330&v="; 

        String USER_AGENT ="Mozilla/5.0 (Linux; U; Android 2.2.1; en-gb; GT-I9003 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";

        HttpClient lClient = new DefaultHttpClient();
        HttpGet lGetMethod = new HttpGet(YOUTUBE_VIDEO_INFORMATION_URL + videoId);
        // setting the header as to get the high quality video url 
        lGetMethod.setHeader("User-Agent",UrlConstants.USER_AGENT);
        HttpResponse lResp = lClient.execute(lGetMethod);

        if(lResp == null)
            return null;
        String lInfoStr = EntityUtils.toString(lResp.getEntity(), "UTF-8");

        JSONObject videoInfoJson = new JSONObject(lInfoStr.substring(4,lInfoStr.length()));
        JSONObject content = (JSONObject) videoInfoJson.get("content");
        JSONObject video = (JSONObject) content.get("video");
        JSONArray fmt_stream_map = video.getJSONArray("fmt_stream_map"); 
        String url = fmt_stream_map.getJSONObject(0).getString("url");

        return Uri.parse(url); 
}
like image 179
Sush Avatar answered Dec 07 '25 02:12

Sush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!