Skip to content

Instantly share code, notes, and snippets.

@brol1dev
Created May 9, 2017 19:07
Show Gist options
  • Save brol1dev/6b3d2cec29c62cec05d2761987e7ee8c to your computer and use it in GitHub Desktop.
Save brol1dev/6b3d2cec29c62cec05d2761987e7ee8c to your computer and use it in GitHub Desktop.
Ooyala Android Player - Custom PlayerInfo
package com.ooyala.sample.players;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.ooyala.android.DefaultPlayerInfo;
import com.ooyala.android.Environment;
import com.ooyala.android.OoyalaPlayer;
import com.ooyala.android.OoyalaNotification;
import com.ooyala.android.PlayerDomain;
import com.ooyala.android.PlayerInfo;
import com.ooyala.android.configuration.Options;
import com.ooyala.sample.R;
import com.ooyala.android.skin.OoyalaSkinLayout;
import com.ooyala.android.skin.OoyalaSkinLayoutController;
import com.ooyala.android.skin.configuration.SkinOptions;
import com.ooyala.android.util.SDCardLogcatOoyalaEventsLogger;
import org.json.JSONObject;
import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
/**
* This activity illustrates how you can play basic playback video using the Skin SDK
* you can also play Ooyala and VAST advertisements programmatically
* through the SDK
*
*/
public class OoyalaSkinPlayerActivity extends Activity implements Observer, DefaultHardwareBackBtnHandler {
final String TAG = this.getClass().toString();
String EMBED = null;
String PCODE = null;
String DOMAIN = null;
// Write the sdk events text along with events count to log file in sdcard if the log file already exists
SDCardLogcatOoyalaEventsLogger Playbacklog= new SDCardLogcatOoyalaEventsLogger();
protected OoyalaSkinLayoutController playerLayoutController;
protected OoyalaPlayer player;
// Custom player info that will be passed to the Ooyala Options
class MyPlayerInfo implements PlayerInfo {
Set<String> supportedFormats;
public MyPlayerInfo() {
supportedFormats = new HashSet<>();
// add the normal supported formats for Android
supportedFormats.add("dash");
supportedFormats.add("akamai_hd2_vod_hls");
supportedFormats.add("mp4");
supportedFormats.add("m3u8");
supportedFormats.add("hls");
supportedFormats.add("akamai_hd2_hls");
}
@Override
public Set<String> getSupportedFormats() {
return supportedFormats;
}
@Override
public Set<String> getSupportedProfiles() {
return null;
}
@Override
public int getMaxWidth() {
return -1;
}
@Override
public int getMaxHeight() {
return -1;
}
@Override
public int getMaxBitrate() {
return -1;
}
@Override
public String getUserAgent() {
return null;
}
// we want to tell the server that we want to get elements as if we were using iOS
// we would get different streams because of it
@Override
public String getDevice() {
return "ios";
}
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(getIntent().getExtras().getString("selection_name"));
setContentView(R.layout.player_skin_simple_layout);
EMBED = getIntent().getExtras().getString("embed_code");
PCODE = getIntent().getExtras().getString("pcode");
DOMAIN = getIntent().getExtras().getString("domain");
// Get the SkinLayout from our layout xml
OoyalaSkinLayout skinLayout = (OoyalaSkinLayout)findViewById(R.id.ooyalaSkin);
// Create the OoyalaPlayer, with some built-in UI disabled
PlayerDomain domain = new PlayerDomain(DOMAIN);
// in this options Builder, pass the Player info, it's is the last part of this line
Options options = new Options.Builder().setShowPromoImage(false).setShowNativeLearnMoreButton(false).setUseExoPlayer(true).setPlayerInfo(new MyPlayerInfo()).build();
player = new OoyalaPlayer(PCODE, domain, options);
//Create the SkinOptions, and setup React
JSONObject overrides = createSkinOverrides();
SkinOptions skinOptions = new SkinOptions.Builder().setSkinOverrides(overrides).build();
playerLayoutController = new OoyalaSkinLayoutController(getApplication(), skinLayout, player, skinOptions);
//Add observer to listen to fullscreen open and close events
playerLayoutController.addObserver(this);
player.addObserver(this);
if (player.setEmbedCode(EMBED)) {
//Uncomment for autoplay
//player.play();
}
else {
Log.e(TAG, "Asset Failure");
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
playerLayoutController.onKeyDown(keyCode, event);
return super.onKeyDown(keyCode, event);
}
/** Start DefaultHardwareBackBtnHandler **/
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
/** End DefaultHardwareBackBtnHandler **/
/** Start Activity methods for Skin **/
@Override
protected void onPause() {
super.onPause();
if (playerLayoutController != null) {
playerLayoutController.onPause();
}
Log.d(TAG, "Player Activity Stopped");
if (player != null) {
player.suspend();
}
}
@Override
protected void onResume() {
super.onResume();
if (playerLayoutController != null) {
playerLayoutController.onResume( this, this );
}
Log.d(TAG, "Player Activity Restarted");
if (player != null) {
player.resume();
}
}
@Override
public void onBackPressed() {
if (playerLayoutController != null) {
playerLayoutController.onBackPressed();
} else {
super.onBackPressed();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (playerLayoutController != null) {
playerLayoutController.onDestroy();
}
}
/** End Activity methods for Skin **/
/**
* Create skin overrides to show up in the skin.
* Default commented. Uncomment to show changes to the start screen.
* @return the overrides to apply to the skin.json in the assets folder
*/
private JSONObject createSkinOverrides() {
JSONObject overrides = new JSONObject();
// JSONObject startScreenOverrides = new JSONObject();
// JSONObject playIconStyleOverrides = new JSONObject();
// try {
// playIconStyleOverrides.put("color", "red");
// startScreenOverrides.put("playButtonPosition", "bottomLeft");
// startScreenOverrides.put("playIconStyle", playIconStyleOverrides);
// overrides.put("startScreen", startScreenOverrides);
// } catch (Exception e) {
// Log.e(TAG, "Exception Thrown", e);
// }
return overrides;
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onRestart() {
super.onRestart();
}
/**
* Listen to all notifications from the OoyalaPlayer
*/
@Override
public void update(Observable arg0, Object argN) {
final String arg1 = OoyalaNotification.getNameOrUnknown(argN);
if (arg1 == OoyalaPlayer.TIME_CHANGED_NOTIFICATION_NAME) {
return;
}
if (arg1 == OoyalaPlayer.ERROR_NOTIFICATION_NAME) {
final String msg = "Error Event Received";
if (player != null && player.getError() != null) {
Log.e(TAG, msg, player.getError());
}
else {
Log.e(TAG, msg);
}
return;
}
if (arg1 == OoyalaSkinLayoutController.FULLSCREEN_CHANGED_NOTIFICATION_NAME) {
Log.d(TAG, "Fullscreen Notification received : " + arg1 + " - fullScreen: " + ((OoyalaNotification)argN).getData());
}
// Automation Hook: to write Notifications to a temporary file on the device/emulator
String text="Notification Received: " + arg1 + " - state: " + player.getState();
// Automation Hook: Write the event text along with event count to log file in sdcard if the log file exists
Playbacklog.writeToSdcardLog(text);
Log.d(TAG, "Notification Received: " + arg1 + " - state: " + player.getState());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment