ExoPlayer IMA एक्सटेंशन का इस्तेमाल शुरू करना

ExoPlayer, Android के लिए एक ऐप्लिकेशन लेवल मीडिया प्लेयर है. इस गाइड में बताया गया है कि विज्ञापन और कॉन्टेंट, दोनों के साथ मीडिया स्ट्रीम का अनुरोध करने और उसे चलाने के लिए, ExoPlayer IMA एक्सटेंशन का इस्तेमाल कैसे किया जा सकता है. इस एक्सटेंशन में IMA Android डीएआई SDK टूल की जानकारी शामिल होती है.

यहां एक्सटेंशन के कुछ फ़ायदे बताए गए हैं:

  • IMA को सुविधाओं के साथ जोड़ने के लिए, ज़रूरी कोड को आसान बनाता है.
  • IMA के नए वर्शन को अपडेट करने में लगने वाले समय को कम करता है.

ExoPlayer IMA एक्सटेंशन, HLS और DASH स्ट्रीमिंग प्रोटोकॉल के साथ काम करता है. यहां खास जानकारी दी गई है:

ExoPlayer-IMA एक्सटेंशन स्ट्रीम से जुड़ी सहायता
लाइव स्ट्रीम वीओडी स्ट्रीम
एचएलएस चेकमार्क चेकमार्क
डैश चेकमार्क चेकमार्क

DASH लाइव स्ट्रीम, ExoPlayer-IMA के 1.1.0 और बाद के वर्शन पर काम करती है.

यह गाइड, ExoPlayer की गाइड पर आधारित है. इसमें पूरा ऐप्लिकेशन बनाने और एक्सटेंशन को इंटिग्रेट करने का तरीका बताया गया है. पूरे ऐप्लिकेशन के उदाहरण वाले उदाहरण के लिए, GitHub से ExoPlayerExample देखें.

ज़रूरी शर्तें

नया Android Studio प्रोजेक्ट बनाना

अपना Android Studio प्रोजेक्ट बनाने के लिए, नीचे दिया गया तरीका अपनाएं:

  • Android Studio शुरू करें.
  • नया Android Studio प्रोजेक्ट शुरू करें को चुनें.
  • अपना प्रोजेक्ट चुनें पेज पर, कोई गतिविधि नहीं टेंप्लेट चुनें.
  • आगे बढ़ें पर क्लिक करें.
  • अपना प्रोजेक्ट कॉन्फ़िगर करें पेज पर, अपने प्रोजेक्ट को नाम दें और भाषा के लिए Java चुनें.

  • पूरा करें पर क्लिक करें.

अपने प्रोजेक्ट में ExoPlayer IMA एक्सटेंशन जोड़ें

dependencies सेक्शन में, ऐप्लिकेशन-लेवल की build.gradle फ़ाइल में एक्सटेंशन के लिए इंपोर्ट जोड़ें.

अपने ऐप्लिकेशन को multidex के लिए कॉन्फ़िगर करें और इसे चालू करें. यह एक्सटेंशन के साइज़ की वजह से ज़रूरी है. साथ ही, यह उन ऐप्लिकेशन के लिए ज़रूरी है जिनमें minSdkVersion को Android 4.4W (एपीआई लेवल 20) या उससे पहले के वर्शन पर सेट किया गया है.

यहां एक उदाहरण दिया गया है:

app/build.gradle

android {

  ...

  defaultConfig {
      applicationId "com.google.ads.interactivemedia.v3.samples.videoplayerapp"
      minSdkVersion 19
      targetSdkVersion 34
      multiDexEnabled true
      versionCode 1
      versionName "1.0"
  }

    ...
}
dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.media3:media3-ui:1.1.1'
    implementation 'androidx.media3:media3-exoplayer:1.1.1'
    implementation 'androidx.media3:media3-exoplayer-hls:1.1.1'
    implementation 'androidx.media3:media3-exoplayer-dash:1.1.1'

    // Adding the ExoPlayer IMA extension for ads will also include the IMA
    // SDK as a dependency.
    implementation 'androidx.media3:media3-exoplayer-ima:1.1.1'
}

विज्ञापनों का अनुरोध करने के लिए, IMA SDK के पास ज़रूरी उपयोगकर्ता अनुमतियां जोड़ें:

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project name">

    <!-- Required permissions for the IMA SDK -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    ...

</manifest>

इंटेंट का एलान जोड़ें

अगर आपका ऐप्लिकेशन, Android 11 (एपीआई लेवल 30) या उसके बाद के वर्शन को टारगेट करता है, तो IMA SDK के मौजूदा और हाल ही के वर्शन को वेब लिंक खोलने के लिए, इंटेंट के बारे में साफ़ तौर पर जानकारी देना ज़रूरी है. विज्ञापन क्लिकथ्रू (ज़्यादा जानें बटन पर क्लिक करने वाले उपयोगकर्ता) चालू करने के लिए, अपने ऐप्लिकेशन की मेनिफ़ेस्ट फ़ाइल में नीचे दिया गया स्निपेट जोड़ें.

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.project name">

      ...

    </application>

    <queries>
      <intent>
          <action android:name="android.intent.action.VIEW" />
          <data android:scheme="https" />
      </intent>
      <intent>
          <action android:name="android.intent.action.VIEW" />
          <data android:scheme="http" />
      </intent>
    </queries>
  </manifest>

ExoPlayer का यूज़र इंटरफ़ेस (यूआई) सेट अप करें

ऐसा PlayerView ऑब्जेक्ट बनाएं जिसका इस्तेमाल ExoPlayer में किया जा सके.

androidx.constraintlayout.widget.ConstraintLayout को LinearLayout में बदलें, जो ExoPlayer IMA एक्सटेंशन के लिए सुझाया गया है.

यहां एक उदाहरण दिया गया है:

app/src/main/res/layout/activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@android:color/black"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyActivity"
    tools:ignore="MergeRootFrame">

    <androidx.media3.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

स्ट्रीम के पैरामीटर जोड़ना

सैंपल स्ट्रीम ऐसेट से जुड़े IMA सैंपल स्ट्रीम पेज पर जाकर, अपने प्रोजेक्ट की जांच करें. साथ ही, अपनी स्ट्रीम सेट अप करने के बारे में जानकारी के लिए, डीएआई पर Ad Manager सेक्शन देखें.

इस चरण में लाइव स्ट्रीम सेट अप करने के बारे में बताया गया है. हालांकि, ExoPlayer IMA एक्सटेंशन, डीएआई वीओडी स्ट्रीम के साथ भी काम करता है. वीओडी स्ट्रीम को मैनेज करने के लिए, आपके ऐप्लिकेशन में किन बदलावों की ज़रूरत है, यह जानने के लिए मांग पर वीडियो (वीडियो ऑन डिमांड) स्ट्रीम करने का तरीका देखें.

ExoPlayer IMA एक्सटेंशन इंपोर्ट करें

ExoPlayer एक्सटेंशन के लिए इंपोर्ट स्टेटमेंट जोड़ें.

MyActivity.java में ये निजी वैरिएबल जोड़ें:

इस स्ट्रीम के साथ टेस्ट करने के लिए, बिग बक बनी (लाइव) HLS स्ट्रीम की एसेट कुंजी जोड़ें. IMA के सैंपल स्ट्रीम पेज पर, जांच के लिए और स्ट्रीम उपलब्ध हैं.

AdsLoader स्टेट को सेव और वापस पाने के लिए, KEY_ADS_LOADER_STATE कॉन्सटेंट बनाएं.

यहां एक उदाहरण दिया गया है:

app/src/main/java/com/example/project name/MyActivity.java


import static androidx.media3.common.C.CONTENT_TYPE_HLS;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DefaultDataSource;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.ima.ImaServerSideAdInsertionMediaSource;
import androidx.media3.exoplayer.ima.ImaServerSideAdInsertionUriBuilder;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.util.EventLogger;
import androidx.media3.ui.PlayerView;
import androidx.multidex.MultiDex;

...

public class MyActivity extends Activity {

  private static final String KEY_ADS_LOADER_STATE = "ads_loader_state";
  private static final String SAMPLE_ASSET_KEY = "c-rArva4ShKVIAkNfy6HUQ";

  private PlayerView playerView;
  private ExoPlayer player;
  private ImaServerSideAdInsertionMediaSource.AdsLoader adsLoader;
  private ImaServerSideAdInsertionMediaSource.AdsLoader.State adsLoaderState;

}

adsLoader इंस्टेंस बनाएं

PlayerView ढूंढने के लिए, onCreate तरीके को ओवरराइट करें और सेव किया गया AdsLoader.State खोजें. इसका इस्तेमाल adsLoader ऑब्जेक्ट को शुरू करते समय किया जा सकता है.

साथ ही, अगर ज़रूरी हो, तो अपने ऐप्लिकेशन के तरीकों की संख्या और minSdkVersion के हिसाब से मल्टीडेक्स चालू करें (जैसा कि दूसरे चरण में बताया गया है).

यहां एक उदाहरण दिया गया है:

app/src/main/java/com/example/project name/MyActivity.java

...

public class MyActivity extends Activity {

  private static final String KEY_ADS_LOADER_STATE = "ads_loader_state";
  private static final String SAMPLE_ASSET_KEY = "c-rArva4ShKVIAkNfy6HUQ";

  private PlayerView playerView;
  private ExoPlayer player;
  private ImaServerSideAdInsertionMediaSource.AdsLoader adsLoader;
  private ImaServerSideAdInsertionMediaSource.AdsLoader.State adsLoaderState;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    MultiDex.install(this);

    playerView = findViewById(R.id.player_view);

    // Checks if there is a saved AdsLoader state to be used later when
    // initiating the AdsLoader.
    if (savedInstanceState != null) {
      Bundle adsLoaderStateBundle = savedInstanceState.getBundle(KEY_ADS_LOADER_STATE);
      if (adsLoaderStateBundle != null) {
        adsLoaderState =
            ImaServerSideAdInsertionMediaSource.AdsLoader.State.CREATOR.fromBundle(
                adsLoaderStateBundle);
      }
    }
  }

}

प्लेयर को शुरू करने के तरीके जोड़ें

प्लेयर शुरू करने के लिए कोई तरीका जोड़ें और ये काम करें:

  • AdsLoader इंस्टेंस बनाएं.
  • ExoPlayer बनाएं.
  • लाइव स्ट्रीम की एसेट कुंजी की मदद से MediaItem बनाएं.
  • अपने प्लेयर पर MediaItem को सेट करें.

यहां एक उदाहरण दिया गया है:

app/src/main/java/com/example/project name/MyActivity.java

public class MyActivity extends Activity {

  ...

  
  // Create a server side ad insertion (SSAI) AdsLoader.
  private ImaServerSideAdInsertionMediaSource.AdsLoader createAdsLoader() {
    ImaServerSideAdInsertionMediaSource.AdsLoader.Builder adsLoaderBuilder =
        new ImaServerSideAdInsertionMediaSource.AdsLoader.Builder(this, playerView);

    // Attempt to set the AdsLoader state if available from a previous session.
    if (adsLoaderState != null) {
      adsLoaderBuilder.setAdsLoaderState(adsLoaderState);
    }

    return adsLoaderBuilder.build();
  }

  private void initializePlayer() {
    adsLoader = createAdsLoader();

    // Set up the factory for media sources, passing the ads loader.
    DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(this);
    DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(dataSourceFactory);

    // MediaSource.Factory to create the ad sources for the current player.
    ImaServerSideAdInsertionMediaSource.Factory adsMediaSourceFactory =
        new ImaServerSideAdInsertionMediaSource.Factory(adsLoader, mediaSourceFactory);

    // 'mediaSourceFactory' is an ExoPlayer component for the DefaultMediaSourceFactory.
    // 'adsMediaSourceFactory' is an ExoPlayer component for a MediaSource factory for IMA server
    // side inserted ad streams.
    mediaSourceFactory.setServerSideAdInsertionMediaSourceFactory(adsMediaSourceFactory);

    // Create an ExoPlayer and set it as the player for content and ads.
    player = new ExoPlayer.Builder(this).setMediaSourceFactory(mediaSourceFactory).build();
    playerView.setPlayer(player);
    adsLoader.setPlayer(player);

    // Build an IMA SSAI media item to prepare the player with.
    Uri ssaiLiveUri =
        new ImaServerSideAdInsertionUriBuilder()
            .setAssetKey(SAMPLE_ASSET_KEY)
            .setFormat(CONTENT_TYPE_HLS) // Use CONTENT_TYPE_DASH for dash streams.
            .build();

    // Create the MediaItem to play, specifying the stream URI.
    MediaItem ssaiMediaItem = MediaItem.fromUri(ssaiLiveUri);

    // Prepare the content and ad to be played with the ExoPlayer.
    player.setMediaItem(ssaiMediaItem);
    player.prepare();

    // Set PlayWhenReady. If true, content and ads will autoplay.
    player.setPlayWhenReady(false);
  }
}

प्लेयर को रिलीज़ करने का कोई तरीका जोड़ें

इस क्रम में प्लेयर को रिलीज़ करने के लिए कोई तरीका जोड़ें:

  • प्लेयर के रेफ़रंस को शून्य पर सेट करें और प्लेयर के संसाधन रिलीज़ करें.
  • adsLoader की स्थिति रिलीज़ करें.

app/src/main/java/com/example/project name/MyActivity.java

public class MyActivity extends Activity {

  ...

  private void releasePlayer() {
    // Set the player references to null and release the player's resources.
    playerView.setPlayer(null);
    player.release();
    player = null;

    // Release the adsLoader state so that it can be initiated again.
    adsLoaderState = adsLoader.release();
  }

खिलाड़ी वाले इवेंट मैनेज करना

आखिर में, स्ट्रीम प्लेबैक को मैनेज करने के लिए, गतिविधि के लाइफ़साइकल इवेंट के लिए कॉलबैक बनाएं.

Android SDK के 24 के बाद के वर्शन पर काम करने के लिए:

Android SDK के 24 से पहले के वर्शन पर काम करने के लिए: - onResume() - onPause()

onStart() और onResume() मैप playerView.onResume() और onStop() और onPause() मैप को playerView.onPause() से जोड़ता है.

इस चरण में, adsLoaderState को सेव करने के लिए onSaveInstanceState() इवेंट का भी इस्तेमाल किया जाता है.

app/src/main/java/com/example/project name/MyActivity.java

public class MyActivity extends Activity {

  ...

  @Override
  public void onStart() {
    super.onStart();
    if (Util.SDK_INT > 23) {
      initializePlayer();
      if (playerView != null) {
        playerView.onResume();
      }
    }
  }

  @Override
  public void onResume() {
    super.onResume();
    if (Util.SDK_INT <= 23 || player == null) {
      initializePlayer();
      if (playerView != null) {
        playerView.onResume();
      }
    }
  }

  @Override
  public void onPause() {
    super.onPause();
    if (Util.SDK_INT <= 23) {
      if (playerView != null) {
        playerView.onPause();
      }
      releasePlayer();
    }
  }

  @Override
  public void onStop() {
    super.onStop();
    if (Util.SDK_INT > 23) {
      if (playerView != null) {
        playerView.onPause();
      }
      releasePlayer();
    }
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    // Attempts to save the AdsLoader state to handle app backgrounding.
    if (adsLoaderState != null) {
      outState.putBundle(KEY_ADS_LOADER_STATE, adsLoaderState.toBundle());
    }
  }

  ...

}

वीओडी (वीडियो ऑन डिमांड) स्ट्रीम सेटअप करना (ज़रूरी नहीं)

अगर आपके ऐप्लिकेशन को विज्ञापनों के साथ वीओडी (वीडियो ऑन डिमांड) कॉन्टेंट चलाना ज़रूरी है, तो आपको ये काम करने होंगे:

  1. वीओडी टेस्ट स्ट्रीम के लिए, CMS ID और Video ID जोड़ें.
  2. ImaServerSideAdInsertionUriBuilder() का इस्तेमाल करके, SSAI वीओडी (वीडियो ऑन डिमांड) यूआरआई बनाएं.
  3. इस नए यूआरआई का इस्तेमाल, अपने प्लेयर के मीडिया आइटम के तौर पर करें.

app/src/main/java/com/example/project name/MyActivity.java

public class MyActivity extends Activity {

  private static final String KEY_ADS_LOADER_STATE = "ads_loader_state";
  private static final String SAMPLE_ASSET_KEY = "c-rArva4ShKVIAkNfy6HUQ";
  private static final String SAMPLE_CMS_ID = "2528370";
  private static final String SAMPLE_VIDEO_ID = "tears-of-steel";

  ...

  private void initializePlayer() {

     ...

    Uri ssaiVodUri =
        new ImaServerSideAdInsertionUriBuilder()
            .setContentSourceId(SAMPLE_CMS_ID)
            .setVideoId(SAMPLE_VIDEO_ID)
            .setFormat(CONTENT_TYPE_HLS)
            .build();

    // Create the MediaItem to play, specifying the stream URI.
    MediaItem ssaiMediaItem = MediaItem.fromUri(ssaiVodUri);

    // Prepare the content and ad to be played with the ExoPlayer.
    player.setMediaItem(ssaiMediaItem);
    player.prepare();

    // Set PlayWhenReady. If true, content and ads will autoplay.
    player.setPlayWhenReady(false);
  }

हो गया! अब ExoPlayer IMA एक्सटेंशन की मदद से, मीडिया स्ट्रीम के लिए अनुरोध किया जा रहा है और उसे चलाया जा रहा है. पूरा कोड जानने के लिए, GitHub पर Android डीएआई सैंपल देखें.