Overview
After completing this Quick Start you should be able to:
- Create a project and add the Brightcove Player SDK for Android using Gradle.
- Alter the layout to include a
BrightcoveExoPlayerVideoView
. - Add to the
onCreate()
method to play videos. from different sources.
Audience
Developers who use Android Studio for development and are interested in using the Brightcove Player SDK for Android in an Android app.
Prerequisites
A minimal knowledge of Java and Android app development.
Get ready
Get ready for development by installing Android Studio
- Download the Android Studio application.
- Follow the directions on the pages to install the application.
Create a project
Create a project in Android Studio and then link to the player SDK using Gradle.
Create a project in Android Studio
- Open Android Studio.
- If this is your first time opening Android Studio, select Start a new Android Studio project.
- If you are seeing the menus, select File > New > New Project.
-
Supply values for the Application Name, Company Domain, and Project location. In this quick start the values shown below are used:
Project name - Click Next.
-
Select the Phone and Tablet platform with the default minimum SDK value.
Project platform - Click Next.
- Choose Empty Activity and click Next.
Choose Empty Activity -
Accept the defaults for the activity details and click Finish.
Finish project creation -
Android Studio will work for a while and eventually display the initial state of the project.
Studio project
Utilize Gradle to link to the Brightcove Player SDK for Android
Since Gradle is integrated with Android Studio, you will use it to add the Native SDK for Android to your project.
-
Expand the Gradle Scripts section and open the second
build.gradle
file, which is associated with the current module in your PlayVideos project.Build gradle file -
At the bottom of the
build.gradle
file, do the following:- Add a
repositories
section for the Brightcove Maven repo. - In the
dependencies
section, include the latest version of the Native SDK for Android. Replace the value6.4.0
below with the latest SDK version, which can be found in the Overview: Brightcove Native SDK for Android document.
repositories { maven { url 'http://repo.brightcove.com/releases' } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' implementation "com.brightcove.player:exoplayer2:6.4.0" }
- Add a
- Sync the project to pick up the Gradle file changes.
Code the application
Next, you will write the code to layout the app, build the video list, and play the videos
Define the app's layout
Even with the Empty Activity, you will get a simple TextView
layout. Replace this with the BrightcoveExoPlayerVideoView
, which defines the view for the player.
- Open the file
app/res/layout/activity_main.xml
. -
Click Text at the bottom of the file's tab to see the raw XML.
Layout XML file -
Remove the existing
TextView
element and add aBrightcoveExoPlayerVideoView
section so the resulting XML appears as follows. Note for later use theid
of the view is namedbrightcove_video_view
.<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.brightcove.playvideos.MainActivity"> <com.brightcove.player.view.BrightcoveExoPlayerVideoView android:id="@+id/brightcove_video_view" android:layout_width="match_parent" android:layout_height="280dp" android:layout_gravity="center_horizontal|top"/> </android.support.constraint.ConstraintLayout>
Enable the app to use the Internet
- Open the
app/manifests/AndroidManifest.xml
file. -
Just after the
<application>
code block, but above the ending</manifest>
tag, insert the following to enable Internet access.<uses-permission android:name="android.permission.INTERNET"/>
- Sync the project to rebuild it and pick up these changes.
Create the View and play a video
-
Return to the
MainActivity.java
file. For theMainActivity
class, extend theBrightcovePlayer
class. This provides default lifecycle management for your app.public class MainActivity extends BrightcovePlayer {
-
The
BrightcovePlayer
class is not automatically imported into the project. Press the option + return keys to add theimport
statement for the class.Before:
Importing class After:
Imported class You will do this for all new classes that you add to the project.
-
Locate the
onCreate()
function. Below any existing code in the function, add the following:-
Create an instance of
BrightcoveExoPlayerVideoView
and associate it with the layout. In most cases, you will use the exoplayer view. For details, see the Choosing a Video View document.
public class MainActivity extends BrightcovePlayer { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view);
Don't forget to import the
BrightcoveExoPlayerVideoView
class. -
-
Next, you need to send your Brightcove Player Account ID to Brightcove using the analytics
setAccount()
method.Analytics analytics = brightcoveVideoView.getAnalytics(); analytics.setAccount("your account Id");
-
Optional: If you override the
BrightcoveExoPlayerVideoView
class or do not use the Brightcove player and catalog, you need to send your Video Cloud Publisher ID to Video Cloud Analytics. You can do this by using the analyticssetAccount()
method. This allows you to view data for this app in Video Cloud Analytics.Analytics analytics = brightcoveVideoView.getAnalytics(); analytics.setAccount("your account Id");
-
Create a video object from your video hosted on a remote server. Set the
DeliveryType
to match the type of video you have.Video video = Video.createVideo("https://learning-services-media.brightcove.com/videos/hls/greatblueheron/greatblueheron.m3u8", DeliveryType.HLS);
-
Load a remote image to be used as the poster image before video playback starts.
try { java.net.URI myposterImage = new java.net.URI("https://solutions.brightcove.com/bcls/assets/images/Great-Blue-Heron.png"); video.getProperties().put(Video.Fields.STILL_IMAGE_URI, myposterImage); } catch (URISyntaxException e) { e.printStackTrace(); }
-
Add the video to the view and start video playback.
brightcoveVideoView.add(video); brightcoveVideoView.start();
-
Check to be sure your
onCreate()
method appears as follows:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view); // 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://learning-services-media.brightcove.com/videos/hls/greatblueheron/greatblueheron.m3u8", DeliveryType.HLS); // Load a remote poster image try { java.net.URI myposterImage = new java.net.URI("https://solutions.brightcove.com/bcls/assets/images/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(); }
-
Run or debug the application to see the video playing.
Get and play a video
In this section, you will use the Catalog
class to retrieve a single video from the Video Cloud server, then play it.
The com.brightcove.player.edge.Catalog
class provides asynchronous methods for retrieving videos and playlists from the Brightcove Playback API. This is the latest and recommended API to retrieve content from your Video Cloud library.
Remove unneeded code
- Some code from the previous app is no longer needed. Remove the two lines of code in the
onCreate()
method that add and start the video. -
Confirm your
onCreate()
method appears as follows:public class MainActivity extends BrightcovePlayer { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view); } }
Retrieve a video from the Catalog
- From Video Cloud Studio, collect the following information:
- Account ID
- Video ID
- Policy Key
-
Define your custom values in your project. Open the
res/values/strings.xml
file and update it as follows:<?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>
-
Return to the
MainActivity.java
file and add the following code:- Get the event emitter from the SDK
-
Create an instance of
com.brightcove.player.edge.Catalog
, using your defined values for Account ID and Policy Key from the previous step.
// 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));
-
Use the Catalog's
findVideoByID()
method with your video ID and aVideoListener
for the callback.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 } @Override public void onError(String s) { } });
-
In the
onVideo()
method, add the video tobrightcoveVideoView
, then start the video.// Start playback of the video with start(). @Override public void onVideo(Video video) { brightcoveVideoView.add(video); brightcoveVideoView.start(); }
-
In the
onError()
method throw the error string.@Override public void onError(String s) { throw new RuntimeException(s); }
-
The complete code for your
MainActivity
class should be similar to this:public class MainActivity extends BrightcovePlayer { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view); 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) { brightcoveVideoView.add(video); brightcoveVideoView.start(); } @Override public void onError(String s) { throw new RuntimeException(s); } }); } }
- Run the app to confirm the video plays.
Get and play a playlist
In this section, you will use the Catalog
class to retrieve a playlist from the Video Cloud server, then play the videos in the playlist.
The com.brightcove.player.edge.Catalog
class provides asynchronous methods for retrieving videos and playlists from the Brightcove Playback API. This is the latest and recommended API to retrieve content from your Video Cloud library.
Remove unneeded code
-
Some code from the previous app is no longer needed. Remove the call to the Catalog's
findVideoByID()
method, and the associatedVideoListener
anonymous callback function.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) { brightcoveVideoView.add(video); brightcoveVideoView.start(); } @Override public void onError(String s) { throw new RuntimeException(s); } });
Retrieve a playlist from the Catalog
-
The existing catalog instance will work for retrieving a playlist, so no changes are necessary to these lines of code:
EventEmitter eventEmitter = brightcoveVideoView.getEventEmitter(); Catalog catalog = new Catalog(eventEmitter, getString(R.string.account), getString(R.string.policy));
- In Video Cloud Studio's Media module, select a playlist and copy the Playlist ID.
-
Use the Catalog's
findPlaylistByID()
method using your playlist ID and aPlaylistListener
for the callback.catalog.findPlaylistByID("2764931905001", new PlaylistListener() { @Override public void onPlaylist(Playlist playlist) { } @Override public void onError(String s) { } });
-
In the
onPlaylist()
method, retrieve the videos from the playlist, add all of the videos tobrightcoveVideoView
, then start the first video.@Override public void onPlaylist(Playlist playlist) { brightcoveVideoView.addAll(playlist.getVideos()); brightcoveVideoView.start(); }
-
In the
onError()
method throw the error string.@Override public void onError(String s) { throw new RuntimeException(s); }
-
The complete code for your
MainActivity
class should be similar to this:public class MainActivity extends BrightcovePlayer { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); brightcoveVideoView = (BrightcoveExoPlayerVideoView) findViewById(R.id.brightcove_video_view); EventEmitter eventEmitter = brightcoveVideoView.getEventEmitter(); Catalog catalog = new Catalog(eventEmitter, getString(R.string.account), getString(R.string.policy)); catalog.findPlaylistByID("4845949311001", new PlaylistListener() { @Override public void onPlaylist(Playlist playlist) { brightcoveVideoView.addAll(playlist.getVideos()); brightcoveVideoView.start(); } @Override public void onError(String s) { throw new RuntimeException(s); } }); } }
- Run the app to confirm multiple videos from the playlist play.
You're done! Thanks for working through the Android SDK Quick Start.