android – 使用VideoView播放高品质的Youtube视频

前端之家收集整理的这篇文章主要介绍了android – 使用VideoView播放高品质的Youtube视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个音乐APP,它显示歌词和歌曲信息,还可以播放来自youtube网址的歌曲.

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

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

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

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

它有效,但我遇到了一个问题:它是一个3gp的文件……质量非常糟糕.
我怎样才能获得质量更好的网址? (MP4 / FLV / ECT?)

解决方法

我设法编写了一个获取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;
}

猜你在找的Android相关文章