在onLoaded()方法中获取视频标题

时间:2017-04-13 06:17:31

标签: android youtube youtube-api android-youtube-api

我知道我可以在YoutubePlayer StateChangeListener的onLoaded()方法中获取Video Id,如下所示:

    @Override
    public void onLoaded(String s) {

        // 's' contains Video ID
    }

但现在只是为了实验,我想知道我怎么才能获得视频标题......

更新

    @Override
    public void onLoaded(String s) {

        // 's' contains Video ID

        Toast.makeText(YouTubeViewActivity.this, s, Toast.LENGTH_SHORT).show();

        String youtubeUrl = "https://www.youtube.com/watch?v=" + s;
        Toast.makeText(YouTubeViewActivity.this, youtubeUrl, Toast.LENGTH_SHORT).show();

        try {
            if (youtubeUrl != null) {
                Toast.makeText(YouTubeViewActivity.this, "Found URL", Toast.LENGTH_SHORT).show();

                URL embededURL = new URL("http://www.youtube.com/oembed?url=" + youtubeUrl + "&format=json");

                strTitle = new JSONObject(IOUtils.toString(embededURL)).getString("title");
                Toast.makeText(YouTubeViewActivity.this, strTitle, Toast.LENGTH_SHORT).show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:1)

这里是如何完成的

String video_title;

@Override
public void onLoaded(String s) {

    String youtubeUrl = "https://www.youtube.com/watch?v=" + s;

    try {
        if (youtubeUrl != null) {
            URL embededURL = new URL("http://www.youtube.com/oembed?url=" +
                youtubeUrl + "&format=json");

            video_title = new JSONObject(IOUtils.toString(embededURL)).getString("title");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}