-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAudioSample.cls
158 lines (154 loc) · 7.62 KB
/
AudioSample.cls
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
global class AudioSample extends alexaforce.AlexaForce {
global override alexaforce.Model.AlexaResponseBody handleRequest(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp;
createLog('##In Handler## Received '+req.type, String.valueOf(req));
if (req.type == 'LaunchRequest') {
resp = getLaunchRequestResponse(req);
} else if (req.type == 'IntentRequest') {
resp = getIntentRequestResponse(req);
} else if (req.type == 'SessionEndedRequest') {
resp = getSessionEndedResponse(req);
} else if(req.type == 'AudioPlayer.PlaybackStarted'){
resp = getPlaybackStartedResponse(req);
} else if(req.type == 'AudioPlayer.PlaybackFinished') {
resp = getPlaybackFinishedResponse(req);
} else if(req.type == 'AudioPlayer.PlaybackStopped') {
resp = getPlaybackFinishedResponse(req);
} else if(req.type == 'AudioPlayer.PlaybackNearlyFinished') {
resp = getPlaybackNearlyFinishedResponse(req);
} else {
resp = getDefaultResponse(req);
}
createLog('##In Handler## Responding to '+req.type, String.valueOf(resp));
return resp;
}
private alexaforce.Model.AlexaResponseBody getLaunchRequestResponse(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp = new alexaforce.Model.AlexaResponseBody();
resp.outputSpeech = new alexaforce.Model.AlexaSpeechResponse();
resp.outputSpeech.type = 'SSML';
resp.outputSpeech.ssml = '<speak>';
resp.outputSpeech.ssml += 'Playing the award winning T.P.O. podcast';
resp.outputSpeech.ssml += '</speak>';
resp.card = new alexaforce.Model.AlexaCard();
resp.card.type = 'Standard';
resp.card.title = 'TPO Podcast';
resp.card.content = 'Playing TPO podcast, with Roderick Veelo and Bert Brussen';
resp.card.image = new alexaforce.Model.AlexaImage();
resp.card.image.smallImageUrl = 'https://tpo.nl/wp-content/uploads/2018/09/podcast3.png';
resp.card.image.largeImageUrl = 'https://tpo.nl/wp-content/uploads/2018/09/podcast3.png';
resp.shouldEndSession = true;
alexaforce.Model.AlexaDirective dir = new alexaforce.Model.AlexaDirective();
dir.type = 'AudioPlayer.Play';
dir.playBehavior = 'REPLACE_ALL';
dir.audioItem = new alexaforce.Model.AlexaAudioItem();
dir.audioItem.stream = new alexaforce.Model.AlexaStream();
dir.audioItem.stream.url = 'https://tpo.nl/wp-content/uploads/2019/01/TPO-podcast-101-1.mp3'; // Hard-coded episode
dir.audioItem.stream.streamFormat ='AUDIO_MP3';
dir.audioItem.stream.token = 'tpo101';
dir.audioItem.metadata = new alexaforce.Model.AlexaMetaData();
dir.audioItem.metadata.title = 'TPO Podcast';
dir.audioItem.metadata.subtitle = 'The award winning podcast, with Roderick Veelo and Bert Brussen.';
Directives.setDirective(dir);
return resp;
}
private alexaforce.Model.AlexaResponseBody getPlaybackStartedResponse(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp = new alexaforce.Model.AlexaResponseBody();
resp.shouldEndSession = true;
return resp;
}
private alexaforce.Model.AlexaResponseBody getPlaybackNearlyFinishedResponse(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp = new alexaforce.Model.AlexaResponseBody();
resp.shouldEndSession = true;
return resp;
}
private alexaforce.Model.AlexaResponseBody getPlaybackFinishedResponse(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp = new alexaforce.Model.AlexaResponseBody();
resp.shouldEndSession = true;
alexaforce.Model.AlexaDirective dir = new alexaforce.Model.AlexaDirective();
dir.type = 'AudioPlayer.Stop';
Directives.setDirective(dir);
return resp;
}
//
// Sample session ended handler.
// This will be fired when alexaforce.Model.AlexaRequest.type = 'SessionEndedRequest'
// Fired in cases of errors (self-inflicted session end) or user commands
private alexaforce.Model.AlexaResponseBody getSessionEndedResponse(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp = new alexaforce.Model.AlexaResponseBody();
resp.outputSpeech.type = 'PlainText';
resp.outputSpeech.text = 'Bye!';
resp.shouldEndSession = true;
return resp;
}
private alexaforce.Model.AlexaResponseBody getIntentRequestResponse(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp = new alexaforce.Model.AlexaResponseBody();
//
// Make sure to gracefully handle all incoming intent types
// these are defined in your model. Some are required by Alexa
// for example the 3 below
if(req.intent.name == 'AMAZON.CancelIntent') {
//
// When the user invokes a cancel-word
return getPlaybackFinishedResponse(req);
}
if(req.intent.name == 'AMAZON.HelpIntent') {
//
// This intent should be handled with a skill explanation
return getLaunchRequestResponse(req);
}
if(req.intent.name == 'AMAZON.StopIntent') {
//
// When the user invokes a stop-word
return getPlaybackFinishedResponse(req);
}
if(req.intent.name == 'AMAZON.PauseIntent') {
//
// When the user invokes a stop-word
return getPlaybackFinishedResponse(req);
}
if(req.intent.name == 'AMAZON.ResumeIntent') {
Long offset = context.AudioPlayer.offsetInMilliseconds;
alexaforce.Model.AlexaDirective dir = new alexaforce.Model.AlexaDirective();
dir.type = 'AudioPlayer.Play';
dir.playBehavior = 'REPLACE_ALL';
dir.audioItem = new alexaforce.Model.AlexaAudioItem();
dir.audioItem.stream = new alexaforce.Model.AlexaStream();
dir.audioItem.stream.url = 'https://tpo.nl/wp-content/uploads/2019/01/TPO-podcast-101-1.mp3';
dir.audioItem.stream.streamFormat ='AUDIO_MP3';
dir.audioItem.stream.token = 'tpo94';
dir.audioItem.stream.offsetInMilliseconds = offset;
Directives.setDirective(dir);
}
/*
Audio intent types
AMAZON.CancelIntent
AMAZON.LoopOffIntent
AMAZON.LoopOnIntent
AMAZON.NextIntent
AMAZON.PauseIntent
AMAZON.PreviousIntent
AMAZON.RepeatIntent
AMAZON.ResumeIntent
AMAZON.ShuffleOffIntent
AMAZON.ShuffleOnIntent
AMAZON.StartOverIntent
*/
resp.shouldEndSession = true;
return resp;
}
//
// The default reponse in case an unsupported Request type is presented by Alexa
// Uses a card
private alexaforce.Model.AlexaResponseBody getDefaultResponse(alexaforce.Model.AlexaRequest req) {
alexaforce.Model.AlexaResponseBody resp = new alexaforce.Model.AlexaResponseBody();
resp.outputSpeech = new alexaforce.Model.AlexaSpeechResponse();
resp.outputSpeech.type = 'PlainText';
resp.outputSpeech.text = 'This is the default response of My Sample Skill';
resp.card = new alexaforce.Model.AlexaCard();
resp.card.type = 'Simple';
resp.card.title = 'My Sample AlexaForce Skill';
resp.card.content = 'This is the default response of My Sample AlexaForce Skill';
resp.shouldEndSession = true;
return resp;
}
}