安卓:使用媒體內容

在本主題中,您將學習如何使用 Android 版本的原生 SDK 來擷取和播放您的視訊。

概覽

視訊雲端客戶可以從視訊雲端工作室存取其媒體資料和中繼資料。布萊特灣玩家的客戶將提供其媒體內容的 URL。

視訊雲端客戶

身為視訊雲端客戶,您可以存取儲存在視訊雲端工作室中的媒體資料。有關更多信息,請參見適用於Android代碼示例的Player SDK

擷取媒體資料

您可以使用播放 API,從視訊雲端媒體櫃擷取您的視訊和播放清單資料。如需有關 API 的詳細資訊,請參閱播放 API 概觀文件。

  1. 使用com.brightcove.player.edge.Catalog類方法從Brightcove的Playback API中檢索您的視頻和播放列表。您的請求可以提供視頻/播放清單IDReferenceID .此服務將發出 URL 請求並解析返回的數據。

  2. 對於此要求,您將需要一個原則金鑰。如果您不熟悉原則金鑰,請參閱原則 API 概觀文件。

    這是有關如何使用com.brightcove.player.edge.Catalog類:

    package com.brightcove.player.samples.exoplayer.basic;
    import android.os.Bundle;
    import android.util.Log;
    import com.brightcove.player.edge.Catalog;
    import com.brightcove.player.edge.VideoListener;
    import com.brightcove.player.event.EventEmitter;
    import com.brightcove.player.model.Video;
    import com.brightcove.player.view.BrightcoveExoPlayerVideoView;
    import com.brightcove.player.view.BrightcovePlayer;
    /**
    * This app illustrates how to use the ExoPlayer with the Brightcove
    * Native Player SDK for Android.
    */
    public class MainActivity extends BrightcovePlayer {
    	private final String TAG = this.getClass().getSimpleName();
    	@Override
    
    	protected void onCreate(Bundle savedInstanceState) {
    		setContentView(R.layout.activity_main);
    		brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view);
    		super.onCreate(savedInstanceState);
    
    		// Get the event emitter from the SDK and create a catalog request to fetch a video from the
    		// Brightcove Edge service, given a video id, an account id and a policy key.
    		EventEmitter eventEmitter = brightcoveVideoView.getEventEmitter();
    		Catalog catalog = new Catalog(eventEmitter, getString(R.string.account), getString(R.string.policy));
    
    		catalog.findVideoByID(getString(R.string.videoId), new VideoListener() {
    		  // Add the video found to the queue with add().
    		  // Start playback of the video with start().
    		  @Override
    		  public void onVideo(Video video) {
    		    Log.v(TAG, "onVideo: video = " + video);
    		    brightcoveVideoView.add(video);
    		    brightcoveVideoView.start();
    		  }
    		});
    	}
    }
  1. 視頻對象提供檢索媒體信息的方法,如下所示:
    catalog.findVideoByID(getString(R.string.videoId), new VideoListener() {
      // Add the video found to the queue with add().
      // Start playback of the video with start().
      @
      Override
      public void onVideo(Video video) {
        Log.v(TAG, "onVideo: video = " + video);
        Log.v(TAG, "onVideo: videoID = " + video.getId());
        Log.v(TAG, "onVideo: videoName = " + video.getName());
        Log.v(TAG, "onVideo: videoDescription = " + video.getDescription());
        Log.v(TAG, "onVideo: videoImage = " + video.getStillImageUri());
        Log.v(TAG, "onVideo: sourceCollections = " + video.getSourceCollections());
        SourceCollection dashCollection = video.getSourceCollections().get(DeliveryType.DASH);
        if (dashCollection != null) {
          Set < Source > sources = dashCollection.getSources();
          for (Source source: sources) {
            if (!TextUtils.isEmpty(source.getUrl())) {
              Log.v(TAG, "onVideo: DASH source = " + source.getUrl());
            }
          }
        }
    
        brightcoveVideoView.add(video);
        brightcoveVideoView.start();
      }
    });

    以上Log()方法返回以下媒體信息:

    視訊物件
    視訊物件
  2. 您可能需要查看自定義字段(如果有)是否存在Video目的。將以下代碼添加到onVideo回調方法循環customField地圖。

    catalog.findVideoByID(getString(R.string.videoId), new VideoListener() {
       @
       Override
       public void onVideo(Video video) {
          Map<String, String> customFieldMap = (HashMap<String, String>) video.getProperties().get(Video.Fields.CUSTOM_FIELDS);
         if (customFieldMap != null && customFieldMap.size() > 0) {
           for (Map.Entry<String, String> entry : customFieldMap.entrySet()) {
             Log.v(TAG, "onVideo: Custom fields: Key: " + entry.getKey() + " Value: " + entry.getValue());
           }
         }
         brightcoveVideoView.add(video);
         brightcoveVideoView.start();
      }
    });

    這是您可以從上面的代碼中看到的日誌輸出示例:

    MainActivity: onVideo: Custom fields: Key: genre Value: Action
    MainActivity: onVideo: Custom fields: Key: customlist Value: customListValue1

    請注意,自定義字段可以表示為Strings要么Lists。即使自定義字段可以是List類型,它是一個列表String值,從中選擇一個值來設置字段的值。

經過地理過濾的視頻

適用於Android的Brightcove Player SDK支持經過地理過濾的視頻。

您可以通過兩種方法向視頻添加地理過濾,以控制可以(或不能)在哪些國家/地區觀看視頻:

在您的Android應用中,當您使用Brightcove的邊緣檢索視頻時目錄針對該視頻進行地理過濾的國家/地區的對象(播放API),您應該會看到以下消息:

error { message: Access to this resource is forbidden by access policy.
client_geo: us
error_subcode: CLIENT_GEO
error_code: ACCESS_DENIED }

布萊特灣玩家顧客

身為布萊特灣播放器客戶,您將提供視訊資產的網址。

這是將視頻添加到視頻視圖並開始播放的示例:

import com.brightcove.player.model.DeliveryType;
import com.brightcove.player.model.Video;
import com.brightcove.player.view.BrightcoveExoPlayerVideoView;
import com.brightcove.player.view.BrightcovePlayer;
import com.brightcove.player.analytics.Analytics;
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	BrightcoveVideoView brightcoveVideoView = (BrightcoveVideoView) findViewById(R.id.brightcove_video_view);

	Analytics analytics = brightcoveVideoView.getAnalytics();
	analytics.setAccount("123456789");

	MediaController controller = new MediaController(this);
	brightcoveVideoView.setMediaController(controller);
	brightcoveVideoView.add(Video.createVideo("http://solutions.brightcove.com/bcls/assets/videos/Bird_Titmouse.mp4", DeliveryType.MP4));
	brightcoveVideoView.start();
}
 

接下來,讓我們看一下事件如何在SDK架構內工作。