如何创建在我的Andr​​oid标签一个YouTube应用? [英] How to create a youtube app in my android Tab?

查看:116
本文介绍了如何创建在我的Andr​​oid标签一个YouTube应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我没有flash播放器从YouTube本身播放视频,我需要在我的MediaPlayer默认发挥它。在code我用如下:

 的MediaController MC =新的MediaController(CTX);
        的setContentView(R.layout.main);
        VV =(VideoView)findViewById(R.id.VideoView01);
        尝试{            UR = Uri.parse(URL / * +&放大器; FMT = 18* /); //&放大器; FMT = 18转换为MP4
            的System.out.println(主机=+ ur.getHost());
            的System.out.println(恩codeD路径=+ ur.getEn codeDPATH());            vv.setVideoURI(UR);
            // vv.setVideoPath(http://www.daily3gp.com/vids/747.3gp);
            vv.setMediaController(MC);
            vv.requestFocus();
            vv.start();
            mc.show();        }赶上(例外前){
            的System.out.println(异常!!!!!!!!!!!!!!!!
                    + ex.getMessage());
        }

问题是....这是获得链接,当我们给的链接播放器,它说的这个视频无法播放.....

请帮助!!!!!!!!!!!!


解决方案

 私有静态无效播放(字符串VIDEOID,INT格式字符串编码,
        字符串的userAgent,文件outputdir,字符串扩展)
        抛出的Throwable
    log.fine(检索+ VIDEOID);
    清单<&的NameValuePair GT; qparams =新的ArrayList<&的NameValuePair GT;();
    qparams.add(新BasicNameValuePair(VIDEO_ID,VIDEOID));
    qparams.add(新BasicNameValuePair(FMT,+格式));
    URI URI = getUri(get_video_info,qparams);
    的System.out.println(************ JavaYoutubeDownloade.play()乌里=
            + uri.toString());
    的System.out.println(JavaYoutubeDownloade.play()用户代理=
            +的userAgent);
    的CookieStore的CookieStore =新BasicCookieStore();
    HttpContext的localContext =新BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE,的CookieStore);    HttpClient的HttpClient的=新DefaultHttpClient();
    HTTPGET HTTPGET =新HTTPGET(URI);
    如果(的userAgent =空&放大器;!&放大器; userAgent.length()大于0){
        httpget.setHeader(用户代理的userAgent);
    }    log.finer(执行+ URI);
    HTT presponse响应= httpclient.execute(HTTPGET,localContext);
    HttpEntity实体= response.getEntity();
    如果(实体= NULL&放大器;!&安培; response.getStatusLine()的getStatus code()== 200){
        InputStream的河道= entity.getContent();
        字符串videoInfo = getStringFromInputStream(编码,插播广告);
        如果(videoInfo =空&放大器;!&放大器; videoInfo.length()大于0){
            清单<&的NameValuePair GT; infoMap =新的ArrayList<&的NameValuePair GT;();
            URLEn codedUtils
                    .parse(infoMap,新的扫描仪(videoInfo),编码);
            字符串downloadUrl = NULL;
            文件名= VIDEOID;            对(对的NameValuePair:infoMap){
                字符串键= pair.getName();
                串VAL = pair.getValue();
                log.finest(键+=+ VAL);
                如果(key.equals(标题)){
                    文件名= VAL;
                }否则如果(key.equals(fmt_url_map)){
                    的String []格式= commaPattern.split(VAL);
                    布尔发现= FALSE;
                    对于(FMT字符串:格式){
                        的String [] = fmtPieces pipePattern.split(FMT);
                        如果(fmtPieces.length == 2){
                            INT pieceFormat =整数
                                    .parseInt(fmtPieces [0]);
                            log.fine(可用格式=+ pieceFormat);
                            如果(pieceFormat ==格式){
                                //找到我们想要的
                                downloadUrl = fmtPieces [1];
                                发现= TRUE;
                                打破;
                            }
                        }
                    }
                    如果(!找到){
                        log.warning(找不到指定的视频格式匹配,但是视频的某些格式确实存在(使用-verbose)。);
                    }
                }
            }            文件名= cleanFilename(文件名);
            如果(filename.length()== 0){
                文件名= VIDEOID;
            }其他{
                文件名+ =_+ VIDEOID;
            }
            文件名+ =。 +扩展;
            文件OUTPUTFILE =新的文件(outputdir,文件名);
            如果(!outputfile.exists()){
                outputfile.createNewFile();            }
            // downloadedFile = outputdir.getPath()+/+文件名;            如果(downloadUrl!= NULL){
                downloadWithHttpClient(的userAgent,downloadUrl,OUTPUTFILE);            }其他{
                log.severe(找不到视频);
            }
        }其他{
            log.severe(没有收到来自YouTube的内容);
        }
    }其他{
        log.severe(无法与YouTube联络:+ response.getStatusLine());
    }
}私有静态无效downloadWithHttpClient(字符串的userAgent,
        串downloadUrl,文件OUTPUTFILE)抛出的Throwable
    HTTPGET httpget2 =新HTTPGET(downloadUrl);
    如果(的userAgent =空&放大器;!&放大器; userAgent.length()大于0){
        httpget2.setHeader(用户代理的userAgent);
    }    log.finer(执行+ httpget2.getURI());
    HttpClient的httpclient2 =新DefaultHttpClient();
    HTT presponse响应2 = httpclient2.execute(httpget2);
    HttpEntity ENTITY2 = response2.getEntity();
    如果(ENTITY2 = NULL&放大器;!&安培; response2.getStatusLine()的getStatus code()== 200){
        双长度= entity2.getContentLength();
        如果(长度下; = 0){
            //意外,但不除以零
            长度= 1;
        }
        InputStream的instream2 = entity2.getContent();        的System.out.println(写
                + commaFormatNo precision.format(长度)+字节
                + OUTPUTFILE);
        如果(outputfile.exists()){
            outputfile.delete();
        }        FileOutputStream中outstream =新的FileOutputStream(OUTPUTFILE);
        尝试{
            字节[]缓冲区=新的字节[BUFFER_SIZE];
            双总= 0;
            诠释计数= -1;
            诠释进度= 10;
            长启动= System.currentTimeMillis的();
            而((计数= instream2.read(缓冲液))!= - 1){
                总+ =计数;
                INT P =(INT)((总/长)* ONE_HUNDRED);
                如果(P&GT =进展){
                    长今= System.currentTimeMillis的();
                    双S =(现在 - 开始)/ 1000;
                    INT kbpers =(INT)((总/ KB)/ S);
                    的System.out.println(进度+%(+ kbpers +KB /秒));
                    进度+ = 10;
                }
                outstream.write(缓冲,0,计数);
            }
            outstream.flush();
        } {最后
            outstream.close();
        }
        的System.out.println(完成);
    }
}

Atfirst,我给下载的网址是不正确的。现在,它正在...

Since I don't have a flash player to play the video from Youtube itself, I need to play it in my default MediaPlayer. The code I used is as follows:

MediaController mc = new MediaController(ctx);
        setContentView(R.layout.main);
        vv = (VideoView) findViewById(R.id.VideoView01);
        try {

            ur = Uri.parse(Url /*+ "&fmt=18"*/); // "&fmt=18"to convert to mp4
            System.out.println("Host = " + ur.getHost());
            System.out.println("Encoded Path = " + ur.getEncodedPath());

            vv.setVideoURI(ur);
            // vv.setVideoPath("http://www.daily3gp.com/vids/747.3gp");
            vv.setMediaController(mc);
            vv.requestFocus();
            vv.start();
            mc.show();

        } catch (Exception ex) {
            System.out.println("Exception!!!!!!!!!!!!!!!! "
                    + ex.getMessage());
        }

The thing is....It is getting the link and when we give the link to the player, it say's This Video cannot be Played.....

Please help !!!!!!!!!!!!

解决方案

private static void play(String videoId, int format, String encoding,
        String userAgent, File outputdir, String extension)
        throws Throwable {
    log.fine("Retrieving " + videoId);
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("video_id", videoId));
    qparams.add(new BasicNameValuePair("fmt", "" + format));
    URI uri = getUri("get_video_info", qparams);
    System.out.println("************JavaYoutubeDownloade.play() Uri = "
            + uri.toString());
    System.out.println("JavaYoutubeDownloade.play() User Agent = "
            + userAgent);
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(uri);
    if (userAgent != null && userAgent.length() > 0) {
        httpget.setHeader("User-Agent", userAgent);
    }

    log.finer("Executing " + uri);
    HttpResponse response = httpclient.execute(httpget, localContext);
    HttpEntity entity = response.getEntity();
    if (entity != null && response.getStatusLine().getStatusCode() == 200) {
        InputStream instream = entity.getContent();
        String videoInfo = getStringFromInputStream(encoding, instream);
        if (videoInfo != null && videoInfo.length() > 0) {
            List<NameValuePair> infoMap = new ArrayList<NameValuePair>();
            URLEncodedUtils
                    .parse(infoMap, new Scanner(videoInfo), encoding);
            String downloadUrl = null;
            filename = videoId;

            for (NameValuePair pair : infoMap) {
                String key = pair.getName();
                String val = pair.getValue();
                log.finest(key + "=" + val);
                if (key.equals("title")) {
                    filename = val;
                } else if (key.equals("fmt_url_map")) {
                    String[] formats = commaPattern.split(val);
                    boolean found = false;
                    for (String fmt : formats) {
                        String[] fmtPieces = pipePattern.split(fmt);
                        if (fmtPieces.length == 2) {
                            int pieceFormat = Integer
                                    .parseInt(fmtPieces[0]);
                            log.fine("Available format=" + pieceFormat);
                            if (pieceFormat == format) {
                                // found what we want
                                downloadUrl = fmtPieces[1];
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found) {
                        log.warning("Could not find video matching specified format, however some formats of the video do exist (use -verbose).");
                    }
                }
            }

            filename = cleanFilename(filename);
            if (filename.length() == 0) {
                filename = videoId;
            } else {
                filename += "_" + videoId;
            }
            filename += "." + extension;


            File outputfile = new File(outputdir, filename);
            if (!outputfile.exists()) {
                outputfile.createNewFile();

            }
            //downloadedFile = outputdir.getPath() + "/" + filename;

            if (downloadUrl != null) {
                downloadWithHttpClient(userAgent, downloadUrl, outputfile);

            } else {
                log.severe("Could not find video");
            }
        } else {
            log.severe("Did not receive content from youtube");
        }
    } else {
        log.severe("Could not contact youtube: " + response.getStatusLine());
    }
}

private static void downloadWithHttpClient(String userAgent,
        String downloadUrl, File outputfile) throws Throwable {
    HttpGet httpget2 = new HttpGet(downloadUrl);
    if (userAgent != null && userAgent.length() > 0) {
        httpget2.setHeader("User-Agent", userAgent);
    }

    log.finer("Executing " + httpget2.getURI());
    HttpClient httpclient2 = new DefaultHttpClient();
    HttpResponse response2 = httpclient2.execute(httpget2);
    HttpEntity entity2 = response2.getEntity();
    if (entity2 != null && response2.getStatusLine().getStatusCode() == 200) {
        double length = entity2.getContentLength();
        if (length <= 0) {
            // Unexpected, but do not divide by zero
            length = 1;
        }
        InputStream instream2 = entity2.getContent();

        System.out.println("Writing "
                + commaFormatNoPrecision.format(length) + " bytes to "
                + outputfile);
        if (outputfile.exists()) {
            outputfile.delete();
        }

        FileOutputStream outstream = new FileOutputStream(outputfile);
        try {
            byte[] buffer = new byte[BUFFER_SIZE];
            double total = 0;
            int count = -1;
            int progress = 10;
            long start = System.currentTimeMillis();
            while ((count = instream2.read(buffer)) != -1) {
                total += count;
                int p = (int) ((total / length) * ONE_HUNDRED);
                if (p >= progress) {
                    long now = System.currentTimeMillis();
                    double s = (now - start) / 1000;
                    int kbpers = (int) ((total / KB) / s);
                    System.out.println(progress + "% (" + kbpers + "KB/s)");
                    progress += 10;
                }
                outstream.write(buffer, 0, count);
            }
            outstream.flush();
        } finally {
            outstream.close();
        }
        System.out.println("Done");
    }
}

Atfirst, the URL that I gave for download was incorrect. Now, it is working...

这篇关于如何创建在我的Andr​​oid标签一个YouTube应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆