在 android 视频视图中流式传输视频

Posted

技术标签:

【中文标题】在 android 视频视图中流式传输视频【英文标题】:streaming video in android video view 【发布时间】:2013-04-16 08:13:09 【问题描述】:

我想在视频视图中播放视频,我问如何将 url 转换为 rstp 我收到了将 url 转换为 rstp 的好人代码。 实施此代码时,我收到质量差的视频。

当我搜索如何修复质量时,我喜欢这个链接 Get the download URL for YouTube video (android/Java)

我的问题是如何让这段代码工作...... 我将此代码复制到我的 Eclipse 并在这些行中收到错误编译器:

ArrayList 视频 = 新的 ArrayList();

progressDialog = ProgressDialog.show(ARViewer.this, "", "正在连接到 YouTube...", true);

任何建议如何解决此错误???? 这对我很有帮助……

谢谢...

【问题讨论】:

请具体说明您遇到的编译器错误?您也可以尝试清理并构建项目。 这是一个编译器错误,我无法运行此代码,在这一行: ArrayList 这个链接会帮助你***.com/questions/16005009/… 我有同样的问题,如果有人找到答案,请分享。这个问题应该怎么解决!? 【参考方案1】:

将 ARViewer.this 替换为 YourActivityName.this,因为 ARViewer 只是代码所在的类名。

【讨论】:

【参考方案2】:

RAViewer It Just Activity Name 用您的 Activity 名称替换 Like 这个

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainLauncher extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_launcher);

        new YouTubePageStreamUriGetter().execute("https://www.youtube.com/watch?v=4GuqB1BQVr4");
    


    class Meta 
        public String num;
        public String type;
        public String ext;

        Meta(String num, String ext, String type) 
            this.num = num;
            this.ext = ext;
            this.type = type;
        
    

    class Video 
        public String ext = "";
        public String type = "";
        public String url = "";

        Video(String ext, String type, String url) 
            this.ext = ext;
            this.type = type;
            this.url = url;
        
    

    public ArrayList<Video> getStreamingUrisFromYouTubePage(String ytUrl)
            throws IOException 
        if (ytUrl == null) 
            return null;
        

        // Remove any query params in query string after the watch?v=<vid> in
        // e.g.
        // http://www.youtube.com/watch?v=0RUPACpf8Vs&feature=youtube_gdata_player
        int andIdx = ytUrl.indexOf('&');
        if (andIdx >= 0) 
            ytUrl = ytUrl.substring(0, andIdx);
        

        // Get the html response
        String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1)";
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                userAgent);
        HttpGet request = new HttpGet(ytUrl);
        HttpResponse response = client.execute(request);
        String html = "";
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
            str.append(line.replace("\\u0026", "&"));
        
        in.close();
        html = str.toString();

        // Parse the HTML response and extract the streaming URIs
        if (html.contains("verify-age-thumb")) 
            Log.e("Downloader","YouTube is asking for age verification. We can't handle that sorry.");
            return null;
        

        if (html.contains("das_captcha")) 
            Log.e("Downloader","Captcha found, please try with different IP address.");
            return null;
        

        Pattern p = Pattern.compile("stream_map\": \"(.*?)?\"");
        // Pattern p = Pattern.compile("/stream_map=(.[^&]*?)\"/");
        Matcher m = p.matcher(html);
        List<String> matches = new ArrayList<String>();
        while (m.find()) 
            matches.add(m.group());
        

        if (matches.size() != 1) 
            Log.e("Downloader","Found zero or too many stream maps.");
            return null;
        

        String urls[] = matches.get(0).split(",");
        HashMap<String, String> foundArray = new HashMap<String, String>();
        for (String ppUrl : urls) 
            String url = URLDecoder.decode(ppUrl, "UTF-8");

            Pattern p1 = Pattern.compile("itag=([0-9]+?)[&]");
            Matcher m1 = p1.matcher(url);
            String itag = null;
            if (m1.find()) 
                itag = m1.group(1);
            

            Pattern p2 = Pattern.compile("sig=(.*?)[&]");
            Matcher m2 = p2.matcher(url);
            String sig = null;
            if (m2.find()) 
                sig = m2.group(1);
            

            Pattern p3 = Pattern.compile("url=(.*?)[&]");
            Matcher m3 = p3.matcher(ppUrl);
            String um = null;
            if (m3.find()) 
                um = m3.group(1);
            

            if (itag != null && sig != null && um != null) 
                foundArray.put(itag, URLDecoder.decode(um, "UTF-8") + "&"
                        + "signature=" + sig);
            
        

        if (foundArray.size() == 0) 
            Log.e("Downloader","Couldn't find any URLs and corresponding signatures");
            return null;
        

        HashMap<String, Meta> typeMap = new HashMap<String,Meta>();
        typeMap.put("13", new Meta("13", "3GP", "Low Quality - 176x144"));
        typeMap.put("17", new Meta("17", "3GP", "Medium Quality - 176x144"));
        typeMap.put("36", new Meta("36", "3GP", "High Quality - 320x240"));
        typeMap.put("5",  new Meta("5", "FLV", "Low Quality - 400x226"));
        typeMap.put("6",  new Meta("6", "FLV", "Medium Quality - 640x360"));
        typeMap.put("34", new Meta("34", "FLV", "Medium Quality - 640x360"));
        typeMap.put("35", new Meta("35", "FLV", "High Quality - 854x480"));
        typeMap.put("43", new Meta("43", "WEBM", "Low Quality - 640x360"));
        typeMap.put("44", new Meta("44", "WEBM", "Medium Quality - 854x480"));
        typeMap.put("45", new Meta("45", "WEBM", "High Quality - 1280x720"));
        typeMap.put("18", new Meta("18", "MP4", "Medium Quality - 480x360"));
        typeMap.put("22", new Meta("22", "MP4", "High Quality - 1280x720"));
        typeMap.put("37", new Meta("37", "MP4", "High Quality - 1920x1080"));
        typeMap.put("33", new Meta("38", "MP4", "High Quality - 4096x230"));

        ArrayList<Video> videos = new ArrayList<Video>();

        for (String format : typeMap.keySet()) 
            Meta meta = typeMap.get(format);

            if (foundArray.containsKey(format)) 
                Video newVideo = new Video(meta.ext, meta.type,
                        foundArray.get(format));
                videos.add(newVideo);
                Log.d("Downloader","YouTube Video streaming details: ext:" + newVideo.ext
                        + ", type:" + newVideo.type + ", url:" + newVideo.url);
            
        

        return videos;
    

    private class YouTubePageStreamUriGetter extends AsyncTask<String, String, String> 
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() 
            super.onPreExecute();
            progressDialog = ProgressDialog.show(MainLauncher.this, "",
                    "Connecting to YouTube...", true);
        

        @Override
        protected String doInBackground(String... params) 
            String url = params[0];
            try 
                ArrayList<Video> videos = getStreamingUrisFromYouTubePage(url);
                if (videos != null && !videos.isEmpty()) 
                    String retVidUrl = null;
                    for (Video video : videos) 
                        if (video.ext.toLowerCase().contains("mp4")
                                && video.type.toLowerCase().contains("medium")) 
                            retVidUrl = video.url;
                            break;
                        
                    
                    if (retVidUrl == null) 
                        for (Video video : videos) 
                            if (video.ext.toLowerCase().contains("3gp")
                                    && video.type.toLowerCase().contains(
                                    "medium")) 
                                retVidUrl = video.url;
                                break;

                            
                        
                    
                    if (retVidUrl == null) 

                        for (Video video : videos) 
                            if (video.ext.toLowerCase().contains("mp4")
                                    && video.type.toLowerCase().contains("low")) 
                                retVidUrl = video.url;
                                break;

                            
                        
                    
                    if (retVidUrl == null) 
                        for (Video video : videos) 
                            if (video.ext.toLowerCase().contains("3gp")
                                    && video.type.toLowerCase().contains("low")) 
                                retVidUrl = video.url;
                                break;
                            
                        
                    

                    return retVidUrl;
                
             catch (Exception e) 
                Log.e("Downloader","Couldn't get YouTube streaming URL", e);
            
            Log.e("Downloader","Couldn't get stream URI for " + url);
            return null;
        

        @Override
        protected void onPostExecute(String streamingUrl) 
            super.onPostExecute(streamingUrl);
            progressDialog.dismiss();
            if (streamingUrl != null) 
                /* Do what ever you want with streamUrl */
            
        
    

【讨论】:

以上是关于在 android 视频视图中流式传输视频的主要内容,如果未能解决你的问题,请参考以下文章

Android 流式音频/视频无法流式传输 rtsp 文件?

Android 从 Google 驱动器流式传输视频

将设备上的视频流式传输到 videoview - Android

将视频从 Raspberry Pi 流式传输到 Android 应用程序

以编程方式将视频从 php 服务器流式传输到 android

Android:如何流式传输视频