Skip to content

Commit

Permalink
add shortcut for initializing player with metrics service already att…
Browse files Browse the repository at this point in the history
…ached. bump to v1.1.0
  • Loading branch information
bikegriffith committed May 12, 2017
1 parent 0ad23b0 commit 0f0733f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 18 deletions.
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install boxcast-sdk-tvos --save

Import the module and initialize constants
```
import { BoxCastData, BoxCastPlayerMetrics } from 'boxcast-sdk-tvos';
import { BoxCastData, BoxCastPlayer } from 'boxcast-sdk-tvos';
const YOUR_CHANNEL_ID = ' TODO: fill in from dashboard ';
const YOUR_APP_NAME = ' TODO: unique identifier used for analytics ';
Expand All @@ -42,20 +42,13 @@ api.getArchivedBroadcasts(YOUR_CHANNEL_ID).then((broadcasts) => {
});
```

When ready to watch a broadcast, grab the "view" that will contain a playlist for live or on-demand content. Note: the `view.playlist` will not exist for future content or for broadcasts that were missed.
When ready to watch a broadcast, simply initialize a player for the broadcast and call the `present`
method, which will request the playlist from the BoxCast API and present a native tvOS player if available.
```
api.getBroadcastView(broadcast.id).then((view) => {
var player = new Player(),
playlist = new Playlist(),
mediaItem = new MediaItem("video", view.playlist);
player.playlist = playlist;
player.playlist.push(mediaItem);
var metrics = new BoxCastPlayerMetrics(broadcast, view, {HOSTNAME: YOUR_APP_NAME});
metrics.attach(player);
player.present();
});
var player = new BoxCastPlayer(broadcast, {HOSTNAME: YOUR_APP_NAME});
player.present()
.then((tvOSPlayerInstance) => { ... })
.catch((errorMessage) => { ... });
```

The SDK also exposes vendor libraries (Bluebird Promise polyfill, fetch polyfill) for your use as needed.
Expand All @@ -71,3 +64,4 @@ const { Promise, fetch } = vendor;
## Changelog

* v1.0.0: Initial version
* v1.1.0: Add shortcut for initializing Player for broadcast with metrics service already attached
2 changes: 1 addition & 1 deletion dist/boxcast-sdk-tvos.js

Large diffs are not rendered by default.

Binary file modified dist/boxcast-sdk-tvos.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "boxcast-sdk-tvos",
"author": "Mike Griffith <[email protected]>",
"version": "1.0.0",
"version": "1.1.0",
"description": "BoxCast SDK for tvOS/TVML video playback",
"bugs": {
"url": "https://github.com/boxcast/boxcast-sdk-tvos/issues"
Expand Down
37 changes: 37 additions & 0 deletions src/BoxCastPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
BoxCastPlayer.js
BoxCast tvOS/TVML SDK
Copyright (c) 2017 BoxCast. All rights reserved.
*/

import BoxCastPlayerMetrics from './BoxCastPlayerMetrics';
import BoxCastData from './BoxCastData';

export default class BoxCastPlayer {
constructor(broadcast, options={}) {
this._broadcast = broadcast;
this._options = options;
this._api = new BoxCastData(options);
}

present() {
return this._api.getBroadcastView(this._broadcast.id).then((view) => {
if (!view.playlist) {
throw `This broadcast is unavailable for viewing. The status is ${view.status}`;
}
var player = new Player(),
playlist = new Playlist(),
mediaItem = new MediaItem("video", view.playlist);
player.playlist = playlist;
player.playlist.push(mediaItem);

var metrics = new BoxCastPlayerMetrics(broadcast, view, this._options);
metrics.attach(player);

player.present();

return player;
});
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fetch from './fetch';

module.exports = {
BoxCastData: require('./BoxCastData'),
BoxCastPlayer: require('./BoxCastPlayer'),
BoxCastPlayerMetrics: require('./BoxCastPlayerMetrics'),
vendor: {
Promise,
Expand Down
5 changes: 3 additions & 2 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

describe('BoxCast SDK for tvOS', function () {
it('should export data, metrics and vendor classes', function () {
const { BoxCastData, BoxCastPlayerMetrics, vendor } = require('../src');
it('should export data, player, metrics and vendor classes', function () {
const { BoxCastData, BoxCastPlayer, BoxCastPlayerMetrics, vendor } = require('../src');
expect(BoxCastData).toBeTruthy();
expect(BoxCastPlayer).toBeTruthy();
expect(BoxCastPlayerMetrics).toBeTruthy();
expect(vendor.Promise).toBeTruthy();
expect(vendor.fetch).toBeTruthy();
Expand Down
24 changes: 24 additions & 0 deletions tests/player.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import './_mock';
import BoxCastPlayer from '../src/BoxCastPlayer';
import Promise from 'bluebird';

describe('BoxCastPlayer', function () {
// Mock TVML built-ins
global.Player = jest.fn();
global.Playlist = jest.fn();
global.MediaItem = jest.fn();

var broadcast = {id: 1000};
var player = new BoxCastPlayer(broadcast, {HOSTNAME: 'My App for tvOS'});
player._api = {getBroadcastView: jest.fn()};

it('should reject the request if the playlist is not availble', function() {
player._api.getBroadcastView.mockReturnValue(new Promise((res, rej) => res({json: () => {playlist: ''}})));
return expect(player.present()).rejects.toMatch('broadcast is unavailable');
});

it('should initialize a player if the playlist is available', function() {
player._api.getBroadcastView.mockReturnValue(new Promise((res, rej) => res({json: () => {playlist: 'http://cdn.example.com/all.m3u8'}})));
return expect(player.present()).rejects.toMatch('broadcast is unavailable');
});
});

0 comments on commit 0f0733f

Please sign in to comment.