如何在我的 Android 选项卡中创建 YouTube 应用程序?

发布于 2024-10-15 04:59:04 字数 926 浏览 9 评论 0原文

由于我没有 Flash 播放器来播放 Youtube 本身的视频,因此我需要在默认的 MediaPlayer 中播放它。我使用的代码如下:

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

问题是......它正在获取链接,当我们将链接提供给播放器时,它说“此视频无法播放......

请帮忙!!!!!!” !!!!!!

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 !!!!!!!!!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铜锣湾横着走 2024-10-22 04:59:04

正如 CommonsWare 所说:play-youtube-video-in-webview

您无法将它们显示为嵌入的,除非在具有 Flash 的设备上。

但是,如果您可以解析 YouTube 视频详细信息,则可以构建一个 ACTION_VIEW Intent 将其显示在 YouTube 应用程序上...适用于那些 Android 设备具有 YouTube 应用程序的。

希望这有帮助。

As CommonsWare said here: play-youtube-video-in-webview

You cannot show them embedded except perhaps on devices that have Flash.

However, if you can parse out the YouTube video details, you may be able to construct an ACTION_VIEW Intent that will show them on the YouTube application...for those Android devices that have the YouTube application.

Hope this helps.

虫児飞 2024-10-22 04:59:04

尝试这可能对你有用

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com")));

try this may be useful to u

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com")));

⒈起吃苦の倖褔 2024-10-22 04:59:04
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");
    }
}

起初,我提供的下载网址不正确。现在,它正在工作......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文