Search code examples
androidtwittertwitter4jtwitter-streaming-api

Play a video with Twitter4j


I'm building an app for Android to read twitter feed I got an issue to play video from a tweet. Currently my tweet feed is showing the thumb image and when I click on it, if it's an image I show the image and in case of a video I expect to be able to play it.

To load the image, I'm doing:

ImageLoader mImageLoader = new ImageLoader(mActivity);
mImageLoader.DisplayImage(mPicToShowUrl, tweet_pic) ;

mPicToShowUrl is coming from :

mediaEntity.getMediaURL().toString();

mediaEntity is coming from the Twitter4j Status.getExpandedURL

tweet_pic is just an ImageView

The URL get look like : example.com/xxx/status/89284924/video/1 which is mainly unusable on any player as it do not contain something like xxx.com/1.avi

This kind of URL and ImageLoader allow to load an image. I'm looking for the same method for a video

I think the twitter video is more a streaming video

Any idea how to play a video from twitter in an ImageView or any other View ?

Thanks


Solution

  • You need to look in the extended_entities of the response. You'll see something like

    "extended_entities": {
        "media": [
          {
            "id": 567972074346807300,
            "id_str": "567972074346807296",
            "indices": [
              46,
              68
            ],
            "media_url": "http://pbs.twimg.com/ext_tw_video_thumb/567972074346807296/pu/img/uz53Ap4wEah7cV50.jpg",
            "media_url_https": "https://pbs.twimg.com/ext_tw_video_thumb/567972074346807296/pu/img/uz53Ap4wEah7cV50.jpg",
            "url": "http://t.co/cGazAn7H3E",
            "display_url": "pic.twitter.com/cGazAn7H3E",
            "expanded_url": "http://twitter.com/katiemoffat/status/567972190639022080/video/1",
            "type": "video",
            "sizes": {
              "small": {
                "w": 340,
                "h": 340,
                "resize": "fit"
              },
              "thumb": {
                "w": 150,
                "h": 150,
                "resize": "crop"
              },
              "medium": {
                "w": 600,
                "h": 600,
                "resize": "fit"
              },
              "large": {
                "w": 720,
                "h": 720,
                "resize": "fit"
              }
            },
            "video_info": {
              "aspect_ratio": [
                1,
                1
              ],
              "duration_millis": 6605,
              "variants": [
                {
                  "bitrate": 832000,
                  "content_type": "video/mp4",
                  "url": "https://video.twimg.com/ext_tw_video/567972074346807296/pu/vid/480x480/eU1s1ig_skHgeRjB.mp4"
                },
                {
                  "content_type": "application/x-mpegURL",
                  "url": "https://video.twimg.com/ext_tw_video/567972074346807296/pu/pl/tr7sF7aHBPOCuL8H.m3u8"
                },
                {
                  "bitrate": 832000,
                  "content_type": "video/webm",
                  "url": "https://video.twimg.com/ext_tw_video/567972074346807296/pu/vid/480x480/eU1s1ig_skHgeRjB.webm"
                },
                {
                  "bitrate": 1280000,
                  "content_type": "video/mp4",
                  "url": "https://video.twimg.com/ext_tw_video/567972074346807296/pu/vid/720x720/njkDGgpJBpsTjQD3.mp4"
                },
                {
                  "bitrate": 320000,
                  "content_type": "video/mp4",
                  "url": "https://video.twimg.com/ext_tw_video/567972074346807296/pu/vid/240x240/Gye4gcWtlJq8zXhF.mp4"
                }
              ]
            }
          }
        ]
      },
    

    So, you need to access extended_entities->media->video_info->variants to see all the different sizes and formats of video available.

    You can choose between different sizes of MP4, a WEBM, and a stream - depending on what is suitable for the device you're playing back on.

    (Taken from https://shkspr.mobi/blog/2015/02/working-with-the-twitter-videos-api/ )