【问题标题】:Play High-Quality Youtube Videos Using VideoView使用 VideoView 播放高质量的 Youtube 视频
【发布时间】:2012-11-24 00:27:49
【问题描述】:

我正在开发一个音乐应用程序,它可以显示歌词和歌曲信息,还可以播放来自 youtube URL 的歌曲。

在我的活动中,有一些 TextView(用于歌词/信息)和一个 VideoView(用于 youtube 链接)。

我创建了一个名为 YoutubeVideo 的类,它执行以下操作:

  1. 下载此页面的内容:http://gdata.youtube.com/feeds/api/videos?q=SONG_NAME&v=2&max-results=1,解析其内容并获取 youtube 的 ID(例如 EvuL5jyCHOw)。这是一个 RSS 源页面,用于搜索视频并显示 1 个结果(我需要做的就是更改查询)。
  2. 下载此页面的内容:http://gdata.youtube.com/feeds/api/videos?q=YOUTUBES_ID&format=1&alt=json,解析其内容并获取 rtsp(例如 rtsp://v7.cache3...3gp)。这是一个 XML 文件,其中包含有关视频的一些信息(名称、长度、缩略图,当然还有 - rtsp 链接:)。
  3. 我需要做的就是将 rtsp 设置为 VideoURI:VideoView.setVideoURI(Uri.parse(rtsp)) 并且正在播放视频。

如果你想知道,我会这样使用这个类:new YoutubeVideo(VideoView).setVideo(artist + " " + title);

它可以工作,但我遇到了一个问题:它是一个 3gp 文件......质量真的很差。 如何获得质量更好的网址? (mp4/flv/ect?)

【问题讨论】:

  • 您找到解决问题的方法了吗?你能看看this query
  • 我在考虑另一种方法:flash 对象的 html 代码包含所有总部视频 url。这就是为什么像 IDM 和一些greasemonkey 脚本这样的程序可以让您下载总部视频的原因。你可以看看这个:userscripts.org/scripts/review/25105。这是一个提取总部 url 的 JavaScript 代码。您可以阅读本文并了解如何自己提取总部链接并让用户选择格式!这样,您可以使用 videoview 而不是 webview。我还没有这样做,所以我没有相应的 java 代码:/

标签: android youtube android-videoview


【解决方案1】:

我设法编写了一个函数,该函数获取一个 youtube 视频 html 代码(html 源代码)并返回一个包含所有可用视频链接的数组。然后我选择一个高质量的链接,并显示它而不是可怜的 3gp。

它并不完美,可能会有一些错误和改进:

    protected String[] extractLinks(String result) {
    //The title of the downloaded file
    String title = "bla bla bla";
    // An array of strings that will hold the links (in case of error, it will hold 1 string)
    String[] temper = new String[1];

    try{
        // Extract the "url_encoded_fmt_stream_map" attr from the flash object
        Pattern p = Pattern.compile("url_encoded_fmt_stream_map(.*?);");          
        Matcher m = p.matcher(result);
        List<String> matches = new ArrayList<String>();
        while(m.find()){
            matches.add(m.group().replace("url_encoded_fmt_stream_map=", "").replace(";", ""));                   
        }
        String[] streamMap = null;
        List<String> links = new ArrayList<String>();

        if(matches.get(0) != null &&  matches.get(0) != ""){
            // Split the "url_encoded_fmt_stream_map" into an array of strings that hold the link values
            streamMap = matches.get(0).split("%2C");
            for (int i = 0; i < streamMap.length; i++){
                String url = "";
                String sig = "";

                //Using regex, we will get the video url.
                Pattern p2 = Pattern.compile("url%3D(.*?)%26");       
                Matcher m2 = p2.matcher(streamMap[i]);
                List<String> matches2 = new ArrayList<String>();
                while(m2.find()){
                    // We don't need the "url=...&" part.
                    matches2.add(m2.group().substring(6, m2.group().length() - 3));
                }                     
                url = matches2.get(0);   

                //Using regex again, we will get the video signature.               
                p2 = Pattern.compile("sig%3D(.*?)%26");       
                m2 = p2.matcher(streamMap[i]);
                matches2 = new ArrayList<String>();
                while(m2.find()){
                // We don't need the "sig=...&" part.               
                    matches2.add(m2.group().substring(6, m2.group().length() - 3));
                }                     
                sig = matches2.get(0);   

                //Now lets add a link to our list. Link = url + sig + title.
                links.add(URLDecoder.decode(URLDecoder.decode(url + "&signature=", "UTF-8") + sig + "%26title%3D" + URLDecoder.decode(title, "UTF-8"), "UTF-8"));
            }
        }
        else{
        links.add("No download Links");
        }
        //Now lets make temper point to a array that holds the list items (aka links).
        temper = links.toArray(new String[links.size()]);
    }
    catch(Exception ex){
    temper[0] = ex.getMessage();
    }
    // That's it! temper has direct download links from youtube!
    return temper;
}

【讨论】:

  • 结果到底是什么?
  • 对不起,代码不清楚,这只是为了快速演示。无论如何,结果参数是一个字符串,其中包含 Flash 对象的 js 代码。它看起来像这样:“” 您可以从页面源中提取此标记。
  • 不用担心清晰度...我通常可以弄清楚,但直接 youtube 链接或 rtsp 文件路径都不起作用。你的方法是得到高质量的文件路径还是低质量的 3gp?如果质量很高,您能否在此处更新您的代码以显示完整示例?非常感谢。
  • 此方法获取 youtube 源页面并使用正则表达式提取视频的所有可用质量。以下是 youtube 今天使用的链接示例:jpst.it/x1HZ 在过去 2 年中,flash 对象(以及下载链接)已更改。它现在有一些新参数,这使得这段代码过时了。此代码仍然可以使用,但需要根据新参数进行更改。另外,据我所知,rtsp 只提供 3gp 格式,这非常糟糕。 ATM 我没有时间更改代码,我不再持有完整的项目。对不起:/
【解决方案2】:

我强烈建议您使用Official YouTube Api

请在链接下载示例 zip 文件并查看 FullscreenDemoActivity.java 文件。这实现了你们大多数人想要的。只需单击并以高质量、全屏切换和旋转时自动全屏显示嵌入的 YouTube 视频。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 2015-07-15
    • 2014-02-17
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多