package com.brightcove.playvideos;
import android.os.Bundle;
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 java.net.URISyntaxException;
public class MainActivity extends BrightcovePlayer {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
// Create the video view
brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view);
super.onCreate(savedInstanceState);
// Optional: For Brightcove Player customers to register their apps
Analytics analytics = brightcoveVideoView.getAnalytics();
analytics.setAccount("your account Id");
// Define a video from a remote server
Video video = Video.createVideo("https://sdks.support.brightcove.com/assets/videos/hls/greatblueheron/greatblueheron.m3u8",
DeliveryType.HLS);
// Load a remote poster image
try {
java.net.URI myposterImage = new java.net.URI("https://sdks.support.brightcove.com/assets/images/general/Great-Blue-Heron.png");
video.getProperties().put(Video.Fields.STILL_IMAGE_URI, myposterImage);
} catch (URISyntaxException e) {
e.printStackTrace();
}
// Add video to the view
brightcoveVideoView.add(video);
// Start video playback
brightcoveVideoView.start();
}
}
public class MainActivity extends BrightcovePlayer {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
// Create the video view
brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view);
super.onCreate(savedInstanceState);
}
}
從目錄中檢索視頻
從您的 Video Cloud Studio 帳戶收集以下信息:
帳戶 ID
視訊 ID
原則金鑰
在項目中定義您的自定義值。打開res/values/strings.xml文件並用您的值更新它:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Application name -->
<string name="app_name">PlayVideos</string>
<!-- A sample Brightcove Edge Account ID -->
<string name="account">your account id</string>
<!-- A sample Brightcove Edge Policy Key -->
<string name="policy">your policy key</string>
<!-- A sample Brightcove Video ID -->
<string name="videoId">your video id</string>
</resources>
返回到MainActivity.java文件並從 SDK 獲取事件發射器。
// Get the event emitter from the SDK
EventEmitter eventEmitter = brightcoveVideoView.getEventEmitter();
使用您在上一步中為帳戶 ID 和策略密鑰定義的值,創建目錄請求以從 Brightcove Edge 服務獲取視頻。
// Create a catalog request to fetch a video
String account = getString(R.string.account);
Catalog catalog = new Catalog.Builder(eventEmitter, account)
.setBaseURL(Catalog.DEFAULT_EDGE_BASE_URL)
.setPolicy(getString(R.string.policy))
.build();
// Get the video by ID
catalog.findVideoByID(getString(R.string.videoId), new VideoListener() {
@Override
public void onVideo(Video video) {
// Add video to the view
brightcoveVideoView.add(video);
// Start video playback
brightcoveVideoView.start();
}
});
你的類的完整代碼應該MainActivity類似於這樣:
public class MainActivity extends BrightcovePlayer {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
// Create the video view
brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view);
super.onCreate(savedInstanceState);
// Get the event emitter from the SDK
EventEmitter eventEmitter = brightcoveVideoView.getEventEmitter();
// Create a catalog request to fetch a video
String account = getString(R.string.account);
Catalog catalog = new Catalog.Builder(eventEmitter, account)
.setBaseURL(Catalog.DEFAULT_EDGE_BASE_URL)
.setPolicy(getString(R.string.policy))
.build();
// Get the video by ID
catalog.findVideoByID(getString(R.string.videoId), new VideoListener() {
@Override
public void onVideo(Video video) {
// Add video to the view
brightcoveVideoView.add(video);
// Start video playback
brightcoveVideoView.start();
}
});
}
}
// Get the video by ID
catalog.findVideoByID(getString(R.string.videoId), new VideoListener() {
@Override
public void onVideo(Video video) {
// Add video to the view
brightcoveVideoView.add(video);
// Start video playback
brightcoveVideoView.start();
}
});
從目錄中檢索播放列表
現有目錄實例將可用於檢索播放列表,因此無需更改這些代碼行:
// Get the event emitter from the SDK
EventEmitter eventEmitter = brightcoveVideoView.getEventEmitter();
// Create a catalog request to fetch a video
String account = getString(R.string.account);
Catalog catalog = new Catalog.Builder(eventEmitter, account)
.setBaseURL(Catalog.DEFAULT_EDGE_BASE_URL)
.setPolicy(getString(R.string.policy))
.build();
在Video Cloud Studio的媒體模塊,選擇一個播放列表,然後復制播放清單編號。
打開res/values/strings.xml文件並為您的播放列表 ID 添加一個條目:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Application name -->
<string name="app_name">PlayVideos</string>
<!-- A sample Brightcove Edge Account ID -->
<string name="account">your account id</string>
<!-- A sample Brightcove Edge Policy Key -->
<string name="policy">your policy key</string>
<!-- A sample Brightcove Playlist ID -->
<string name="playlistId">your playlist id</string>
</resources>
// Get the playlist by ID
String playlist = getString(R.string.playlistId);
catalog.findPlaylistByID(playlist, new PlaylistListener() {
@Override
public void onPlaylist(Playlist playlist) {
// Add playlist to the view
brightcoveVideoView.addAll(playlist.getVideos());
// Start playback
brightcoveVideoView.start();
}
});
你的類的完整代碼應該MainActivity類似於這樣:
public class MainActivity extends BrightcovePlayer {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
// Create the video view
brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view);
super.onCreate(savedInstanceState);
// Get the event emitter from the SDK
EventEmitter eventEmitter = brightcoveVideoView.getEventEmitter();
// Create a catalog request to fetch a video
String account = getString(R.string.account);
Catalog catalog = new Catalog.Builder(eventEmitter, account)
.setBaseURL(Catalog.DEFAULT_EDGE_BASE_URL)
.setPolicy(getString(R.string.policy))
.build();
// Get the playlist by ID
String playlist = getString(R.string.playlistId);
catalog.findPlaylistByID(playlist, new PlaylistListener() {
@Override
public void onPlaylist(Playlist playlist) {
// Add playlist to the view
brightcoveVideoView.addAll(playlist.getVideos());
// Start playback
brightcoveVideoView.start();
}
});
}
}