android - 从 android 中的 youtube 视频中抓取帧?

标签 android android-mediaplayer android-youtube-api

我正在使用 videoview 显示以下 youtube 视频

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

我不想看到黑屏,但想显示视频的中帧作为视频的封面。

在IOS中,有

https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVAssetImageGenerator_Class/Reference/Reference.html这有帮助。

在android中如何实现?

最佳答案

这取决于设备的 android 版本和 flash 支持功能。 您不能使用 VideoView setVideoUri 方法播放视频(在将 http 链接解析为 uri 之后),除非您在设备上安装了 flash。

下面的代码对我有用,但这对你管来说是一种破解。
将此代码用于未安装闪存的设备。

 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); 
}

关于android - 从 android 中的 youtube 视频中抓取帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14228860/

相关文章:

安卓工作室 2.0 "waiting for the debugger to attach"

php - 通过移动应用程序为二维码的值添加前缀

android - Acer Iconia A200 上的 VideoView getCurrentPosition() 异常

java - 无法调用资源(特别是字符串数组) - java/android

android - 如何阻止媒体播放器自行播放?

android - 在 RecyclerView 中播放视频 - 滚动到屏幕外时播放/暂停视频

android - 应用程序如何显示YouTube视频并仍然符合Google的应用指南?

android - Youtube API-如何强制全屏显示?

android - 使用 VrVideoView 加载 360 度 Youtube 视频

android - 如何通过 id 获取 MenuItem