-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioEventHandlers.js
104 lines (95 loc) · 4.06 KB
/
audioEventHandlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';
var Alexa = require('alexa-sdk');
var audioData = require('./audioAssets');
var constants = require('./constants');
// Binding audio handlers to PLAY_MODE State since they are expected only in this mode.
var audioEventHandlers = Alexa.CreateStateHandler(constants.states.PLAY_MODE, {
'PlaybackStarted' : function () {
/*
* AudioPlayer.PlaybackStarted Directive received.
* Confirming that requested audio file began playing.
* Storing details in dynamoDB using attributes.
*/
this.attributes['token'] = getToken.call(this);
this.attributes['index'] = getIndex.call(this);
this.attributes['playbackFinished'] = false;
this.emit(':saveState', true);
},
'PlaybackFinished' : function () {
/*
* AudioPlayer.PlaybackFinished Directive received.
* Confirming that audio file completed playing.
* Storing details in dynamoDB using attributes.
*/
this.attributes['playbackFinished'] = true;
this.attributes['enqueuedToken'] = false;
this.emit(':saveState', true);
},
'PlaybackStopped' : function () {
/*
* AudioPlayer.PlaybackStopped Directive received.
* Confirming that audio file stopped playing.
* Storing details in dynamoDB using attributes.
*/
this.attributes['token'] = getToken.call(this);
this.attributes['index'] = getIndex.call(this);
this.attributes['offsetInMilliseconds'] = getOffsetInMilliseconds.call(this);
this.emit(':saveState', true);
},
'PlaybackNearlyFinished' : function () {
/*
* AudioPlayer.PlaybackNearlyFinished Directive received.
* Using this opportunity to enqueue the next audio
* Storing details in dynamoDB using attributes.
* Enqueuing the next audio file.
*/
if (this.attributes['enqueuedToken']) {
/*
* Since AudioPlayer.PlaybackNearlyFinished Directive are prone to be delivered multiple times during the
* same audio being played.
* If an audio file is already enqueued, exit without enqueuing again.
*/
return this.context.succeed({});
}
var enqueueIndex = this.attributes['index'];
enqueueIndex +=1;
// Checking if there are any items to be enqueued.
if (enqueueIndex === audioData.length) {
if (this.attributes['loop']) {
// Enqueueing the first item since looping is enabled.
enqueueIndex = 0;
} else {
// Nothing to enqueue since reached end of the list and looping is disabled.
return this.context.succeed({});
}
}
// Setting attributes to indicate item is enqueued.
this.attributes['enqueuedToken'] = String(this.attributes['playOrder'][enqueueIndex]);
var enqueueToken = this.attributes['enqueuedToken'];
var playBehavior = 'ENQUEUE';
var podcast = audioData[this.attributes['playOrder'][enqueueIndex]];
var expectedPreviousToken = this.attributes['token'];
var offsetInMilliseconds = 0;
this.response.audioPlayerPlay(playBehavior, podcast.url, enqueueToken, expectedPreviousToken, offsetInMilliseconds);
this.emit(':responseReady');
},
'PlaybackFailed' : function () {
// AudioPlayer.PlaybackNearlyFinished Directive received. Logging the error.
console.log("Playback Failed : %j", this.event.request.error);
this.context.succeed({});
}
});
module.exports = audioEventHandlers;
function getToken() {
// Extracting token received in the request.
return this.event.request.token;
}
function getIndex() {
// Extracting index from the token received in the request.
var tokenValue = parseInt(this.event.request.token);
return this.attributes['playOrder'].indexOf(tokenValue);
}
function getOffsetInMilliseconds() {
// Extracting offsetInMilliseconds received in the request.
return this.event.request.offsetInMilliseconds;
}