Use an intent to start exoplayer streaming

We mainly focus on four files, SampleChooserActivity, PlayerActivity, media.exolist.json, AndroidManifest.xml.

  • SampleChooserActivity is the main UI, where we choose a stream to play.
  • media.exolist.json is the file storing the stream info, uri, extension, drm_scheme etc.
  • PlayerActivity receives the data passed in and deal with them.
  • AndroidManifest.xml stores activity info.

In this case, possible stream info are: uri, extension, drm_scheme, drm_license_url, ad_tag_uri.

Step 1: We take a look at AndroidManifest.xml:

<activity android:name="com.google.android.exoplayer2.demo.PlayerActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
        android:launchMode="singleTop"
        android:label="@string/application_name"
        android:theme="@style/PlayerTheme">
      <intent-filter>
        <action android:name="com.google.android.exoplayer.demo.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="http"/>
        <data android:scheme="https"/>
        <data android:scheme="content"/>
        <data android:scheme="asset"/>
        <data android:scheme="file"/>
      </intent-filter>
      <intent-filter>
        <action android:name="com.google.android.exoplayer.demo.action.VIEW_LIST"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
    </activity>

We can see the PlayerActivity receives two kinds of intent:

<intent-filter>
    <action android:name="com.google.android.exoplayer.demo.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    ...
</intent-filter>
<intent-filter>
    <action android:name="com.google.android.exoplayer.demo.action.VIEW_LIST"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Step 2: Let's have a look how the info are passed to PlayerActivity.

The following code is found in this class:

public Intent buildIntent(Context context) {
  Intent intent = new Intent(context, PlayerActivity.class);
  intent.putExtra(PlayerActivity.PREFER_EXTENSION_DECODERS, preferExtensionDecoders);
  if (drmInfo != null) {
    drmInfo.updateIntent(intent);
  }

  return intent;
}
@Override
public Intent buildIntent(Context context) {
    return super.buildIntent(context)
    .setData(Uri.parse(uri))
    .putExtra(PlayerActivity.EXTENSION_EXTRA, extension)
    .putExtra(PlayerActivity.AD_TAG_URI_EXTRA, adTagUri)
    .setAction(PlayerActivity.ACTION_VIEW);
}
@Override
public Intent buildIntent(Context context) {
    String[] uris = new String[children.length];
    String[] extensions = new String[children.length];
    for (int i = 0; i < children.length; i++) {
        uris[i] = children[i].uri;
        extensions[i] = children[i].extension;
    }
    return super.buildIntent(context)
    .putExtra(PlayerActivity.URI_LIST_EXTRA, uris)
    .putExtra(PlayerActivity.EXTENSION_LIST_EXTRA, extensions)
    .setAction(PlayerActivity.ACTION_VIEW_LIST);
}
public void updateIntent(Intent intent) {
    Assertions.checkNotNull(intent);
    intent.putExtra(PlayerActivity.DRM_SCHEME_UUID_EXTRA, drmSchemeUuid.toString());
    intent.putExtra(PlayerActivity.DRM_LICENSE_URL, drmLicenseUrl);
    intent.putExtra(PlayerActivity.DRM_KEY_REQUEST_PROPERTIES, drmKeyRequestProperties);
    intent.putExtra(PlayerActivity.DRM_MULTI_SESSION, drmMultiSession);
}
private UUID getDrmUuid(String typeString) throws ParserException {
    switch (Util.toLowerInvariant(typeString)) {
    case "widevine":
        return C.WIDEVINE_UUID;
    case "playready":
        return C.PLAYREADY_UUID;
    case "clearkey":
        return C.CLEARKEY_UUID;
    default:
        try {
            return UUID.fromString(typeString);
         catch (RuntimeException e) {
            throw new ParserException("Unsupported drm type: " + typeString);
        }
    }
}

This means if the steam we choose contains one single uri, we can pass an intent this way:


        Context context = InstrumentationRegistry.getTargetContext();
        String uri;
        String extension;
        String drm_scheme_uuid;
        String drm_license_url;
        String ad_tag_uri;
        
        Intent intent = new Intent(context, PlayerActivity.class);
        intent.setAction(PlayerActivity.ACTION_VIEW);
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setData(Uri.parse(uri));
        intent.putExtra(PlayerActivity.EXTENSION_EXTRA, extension);
        intent.putExtra(PlayerActivity.DRM_SCHEME_UUID_EXTRA, drm_scheme_uuid);
        intent.putExtra(PlayerActivity.DRM_LICENSE_URL, drm_license_url);
        intent.putExtra(PlayerActivity.AD_TAG_URI_EXTRA, ad_tag_uri)

Or if the stream we choose contains multiple uris, we can pass an intent this way:

        Context context = InstrumentationRegistry.getTargetContext();
        String uri;
        String extension;
        String drm_scheme_uuid;
        String drm_license_url;
        String ad_tag_uri;

        Intent intent = new Intent(context, PlayerActivity.class);
        intent.setAction(PlayerActivity.ACTION_VIEW_LIST);
        intent.addCategory("android.intent.category.DEFAULT");
        intent.putExtra(PlayerActivity.URI_LIST_EXTRA, uris);
        intent.putExtra(PlayerActivity.EXTENSION_EXTRA, extension);
        intent.putExtra(PlayerActivity.DRM_SCHEME_UUID_EXTRA, drm_scheme_uuid);
        intent.putExtra(PlayerActivity.DRM_LICENSE_URL, drm_license_url);
        intent.putExtra(PlayerActivity.AD_TAG_URI_EXTRA, ad_tag_uri)

Step 3: Let's have a look at how to info are received by PlayerActivity:

UUID drmSchemeUuid = intent.hasExtra(DRM_SCHEME_UUID_EXTRA)
? UUID.fromString(intent.getStringExtra(DRM_SCHEME_UUID_EXTRA)) : null;

Intent intent = getIntent();
String action = intent.getAction();

if (ACTION_VIEW.equals(action)) {
      uris = new Uri[]{intent.getData()};
      extensions = new String[]{intent.getStringExtra(EXTENSION_EXTRA)};
    } else if (ACTION_VIEW_LIST.equals(action)) {
      String[] uriStrings = intent.getStringArrayExtra(URI_LIST_EXTRA);
      uris = new Uri[uriStrings.length];
      for (int i = 0; i < uriStrings.length; i++) {
        uris[i] = Uri.parse(uriStrings[i]);
      }
      extensions = intent.getStringArrayExtra(EXTENSION_LIST_EXTRA);
      if (extensions == null) {
        extensions = new String[uriStrings.length];
      }
    } else {
      showToast(getString(R.string.unexpected_intent_action, action));
      return;
    }
UUID drmSchemeUuid = intent.hasExtra(DRM_SCHEME_UUID_EXTRA)?UUID.fromString(intent.getStringExtra(DRM_SCHEME_UUID_EXTRA)) : null;
uris = new Uri[]{intent.getData()
String[] uriStrings = intent.getStringArrayExtra(URI_LIST_EXTRA);
extensions = new String[]{intent.getStringExtra(EXTENSION_EXTRA)};
String drmLicenseUrl = intent.getStringExtra(DRM_LICENSE_URL);
String adTagUriString = intent.getStringExtra(AD_TAG_URI_EXTRA);
In general, intent extras are passed as key value pair: a value for a key, except when we wish to pass a uri, we use Intent.setData(Uri uri).

Additional info, the json file looks like this:

"name": "WV: HDCP not specified",
        "uri": "https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears.mpd",
        "drm_scheme": "widevine",
        "drm_license_url": "https://proxy.uat.widevine.com/proxy?video_id=d286538032258a1c&provider=widevine_test"
{
        "name": "Google Play (WebM,VP9)",
        "uri": "http://www.youtube.com/api/manifest/dash/id/3aa39fa2cc27967f/source/youtube?as=fmp4_audio_clear,webm2_sd_hd_clear&sparams=ip,ipbits,expire,source,id,as&ip=0.0.0.0&ipbits=0&expire=19000000000&signature=B1C2A74783AC1CC4865EB312D7DD2D48230CC9FD.BD153B9882175F1F94BFE5141A5482313EA38E8D&key=ik0",
        "extension": "mpd"
      }

or

 {
        "name": "Clear -> Enc -> Clear -> Enc -> Enc",
        "drm_scheme": "widevine",
        "drm_license_url": "https://proxy.uat.widevine.com/proxy?provider=widevine_test",
        "playlist": [
          {
            "uri": "https://html5demos.com/assets/dizzy.mp4"
          },
          {
            "uri": "https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears_sd.mpd"
          },
          {
            "uri": "https://html5demos.com/assets/dizzy.mp4"
          },
          {
            "uri": "https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears_sd.mpd"
          },
          {
            "uri": "https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears_sd.mpd"
          }
        ]
      }

Of course, the last one needs to be passed in as

Intent.setAction(PlayerActivity.ACTION_VIEW_LIST);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末碘橘,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖间狂,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異踩官,居然都是意外死亡瞬逊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進(jìn)店門忽你,熙熙樓的掌柜王于貴愁眉苦臉地迎上來幼东,“玉大人,你說我怎么就攤上這事檀夹〗畲郑” “怎么了?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵炸渡,是天一觀的道長娜亿。 經(jīng)常有香客問我,道長蚌堵,這世上最難降的妖魔是什么买决? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮吼畏,結(jié)果婚禮上督赤,老公的妹妹穿的比我還像新娘。我一直安慰自己泻蚊,他們只是感情好躲舌,可當(dāng)我...
    茶點故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著性雄,像睡著了一般没卸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上秒旋,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天约计,我揣著相機(jī)與錄音,去河邊找鬼迁筛。 笑死煤蚌,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的细卧。 我是一名探鬼主播尉桩,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贪庙!你這毒婦竟也來了蜘犁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤插勤,失蹤者是張志新(化名)和其女友劉穎沽瘦,沒想到半個月后革骨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡析恋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年良哲,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片助隧。...
    茶點故事閱讀 40,567評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡筑凫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出并村,到底是詐尸還是另有隱情巍实,我是刑警寧澤,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布哩牍,位于F島的核電站棚潦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏膝昆。R本人自食惡果不足惜丸边,卻給世界環(huán)境...
    茶點故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望荚孵。 院中可真熱鬧妹窖,春花似錦、人聲如沸收叶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽判没。三九已至蜓萄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間哆致,已是汗流浹背绕德。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工患膛, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留摊阀,地道東北人。 一個月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓踪蹬,卻偏偏與公主長得像胞此,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子跃捣,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理漱牵,服務(wù)發(fā)現(xiàn),斷路器疚漆,智...
    卡卡羅2017閱讀 134,702評論 18 139
  • 教大家做一款黑米糕: 1酣胀、黑米刁赦、糯米泡過夜晾干,用料理機(jī)打成粉過篩闻镶。 2甚脉、準(zhǔn)備三個雞蛋,分兩個盆蛋清蛋黃分離铆农。 3...
    五月的荷閱讀 310評論 6 10
  • 朋友圈到處都是各種曬春天牺氨,各種小愜意,小幸福墩剖。此刻猴凹,我連哭都沒有地方。
    加冕_I閱讀 128評論 0 0
  • 這絕不是個討好的年紀(jì)——三十歲,尤其當(dāng)它發(fā)生在女人身上爷绘〈趼ǎ——艾明雅 在當(dāng)當(dāng)網(wǎng)下單選購這本書的時候,我的心態(tài)是這樣的...
    余小魚MsYu閱讀 754評論 2 10
  • 我曾經(jīng)也想過一了百了揉阎,被掏空的心已經(jīng)無力承受庄撮。蜷縮在灰暗角落,也不知道伸一伸手...... 曾經(jīng)也想過夢醒了是什...
    話說才才閱讀 279評論 0 0