2011-09-20 136 views
2

我试图实现可以浏览和显示YouTube内容(最重要的是缩略图)的Android应用程序。使用“用于Java的Google API客户端库”访问YouTube视频数据

现在通常情况下,通过Youtube API和Gdata的方式,但不幸的是,它们不能很好地与Android搭配。

因此,我试图使用谷歌API客户端库的Java(http://code.google.com/p/google-api-java-client/)

这显然与Android完全兼容。不幸的是,它的发展还很早,所以没有太多的信息。

我已经成功地查询了YouTube的数据库并返回了标题和描述等数据,但是我在返回缩略图方面遇到了很多麻烦。说实话,我不太清楚查询过程是如何工作的。

这里是代码的主要部分:

HttpTransport transport = new NetHttpTransport();     
     final JsonFactory jsonFactory = new JacksonFactory();  
     final JsonCParser parser = new JsonCParser(jsonFactory); 

     HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() { 

      @Override 
      public void initialize(HttpRequest request) throws IOException { 
       // TODO Auto-generated method stub 

       GoogleHeaders headers = new GoogleHeaders(); 
       headers.setApplicationName("1Google-YouTubeSample/1.0"); 
       headers.gdataVersion = "2"; 
       request.setHeaders(headers); 
       request.addParser(parser); 
      } 
     }); 


     YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos"); 
     url.author = "whatever"; 
     url.maxResults = 2; 

     HttpRequest request; 
     try { 
      request = factory.buildGetRequest(url); 

      VideoFeed feed = request.execute().parseAs(VideoFeed.class); 

      for (Video video : feed.items) { 

       Log.v("ddarz", "video URL" + video.player.defaultUrl + "/default.jpg"); 
       TextView tv1 = (TextView) findViewById(R.id.textView1); 
       tv1.setText(video.title); 

       TextView tv2 = (TextView) findViewById(R.id.textView2); 
       tv2.setText(video.description); 

       ImageView iv = (ImageView) findViewById(R.id.imageView1); 
       Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://img.youtube.com/vi/bQVoAWSP7k4/1.jpg").getContent()); 
       //iv.setImageBitmap(bitmap); 

      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 

     } 

及其类别:

public static class VideoFeed { 
     @Key List<Video> items; 
     int totalItems; 
     } 

     public static class Video { 
     @Key String title; 
     @Key String description; 
     @Key Player player; 
     @Key DateTime updated; 
     } 

     public static class Player { 
     @Key("default") String defaultUrl; 
     } 

     public static class YouTubeUrl extends GenericUrl { 
     @Key final String alt = "jsonc"; 
     @Key String author; 
     @Key("max-results") Integer maxResults; 

     YouTubeUrl(String url) { 
      super(url); 
     } 
     } 

的代码是相当粗糙,但是这一点它的工作原理,它试图获取失败时来自网址的图片。

有没有人有任何关于如何最好地访问视频数据(缩略图等)的洞察力和/或建议或一般如何解决这个问题?不幸的是,Android平台上的YouTube访问很少涉及到问题。

非常感谢提前!

+0

我也一直在检索视频预览缩略图。也许我们可以尝试解析缩略图的请求?不过,我只能看到一个gzip压缩请求,其中包含没有适当的信息:( –

回答

2

您正在使用google api页面中的示例代码。它们仅涵盖YouTube帖子的几个基本要素。

url.alt = "jsonc"; 

这种方式在YouTube API将返回所有的请求在JSON-C format:但是,当您加入这一行,你可以访问更多的信息。您现在可以使用缩略图的新类扩展类VideoFeed。我的是这样:

public static class Thumbnail { 
    @Key("sqDefault") 
    String thumbnail_sq; 
    @Key("hqDefault") 
    String thumbnail_hq; 

    @Override 
    public String toString() { 
     return thumbnail_sq; 
    } 
} 
  • sqDefault:120像素* 90像素
  • hqDefault:480像素* 360像素,

现在我只是在标准质量的缩略图。使用@Key,您可以分配变量,以便Google Api将使用该变量自动将它们解析为来自JSON格式化答案的VideoFeed。您可以使用返回的字符串下载图像并将其显示在ImageView中。

希望这会有所帮助:)

+0

值得注意的是,https://developers.google.com/youtube/2.0/developers_guide_jsonc有两个缩略图列为默认和hqDefault。这使我因为没有缩略图称为默认值,所以有点压力。>。< – Aelexe

相关问题