AudioPlayer functionality (Continue PR #88) #92
Conversation
wschaeferiii and others
added
some commits
Nov 3, 2016
coveralls
commented
Dec 23, 2016
coveralls
commented
Dec 23, 2016
dblock
requested changes
Dec 23, 2016
View changes
I think this is almost there, nice work. Lets finish it up, see my comments.
| @@ -167,6 +167,33 @@ app.sessionEnded(function(request, response) { | ||
| }); | ||
| ``` | ||
| +## AudioPlayer Event Request | ||
| + | ||
| +Define the handler for multiple events using multiple calls to `audioPlayer()`. You can define only one handler per event. Event handlers that don't return an immediate response (because they do some asynchronous operation) must return false. |
dblock
Dec 23, 2016
Collaborator
Maybe add a link to audio player support on some amazon site to explain what this is somewhere?
wschaeferiii
Jan 17, 2017
Hey @fremail , thank you for fixing my sloppy code and adding the AudioPlayer Event Request feature!
I would love to use the feature, however having issues issues with my app listening to AudioPlayer events. When the Alexa sends a audioplayer request (I can log the request_json, so I know its sent), the try / catch block with the var requestType = request.type() isn't even firing in my code.
Am I missing something here?
dblock
Jan 17, 2017
Collaborator
@wschaeferiii I know it's a continuation of this code, but just open a new issue for any bug you encounter against 2.4.0, hopefully with a test that reproduces the problem.
fremail
Jan 19, 2017
Contributor
@wschaeferiii it's hard to suspect why it doesn't work for you. Please create an issue with a code example. Or point me to the issue please.
| +## AudioPlayer Event Request | ||
| + | ||
| +Define the handler for multiple events using multiple calls to `audioPlayer()`. You can define only one handler per event. Event handlers that don't return an immediate response (because they do some asynchronous operation) must return false. | ||
| +You can define handlers for these events: |
dblock
Dec 23, 2016
Collaborator
for the following events ...
And space things out a bit, add an empty line above and below.
| +* PlaybackNearlyFinished | ||
| +* PlaybackFailed | ||
| + | ||
| +See example further below. |
| +}); | ||
| +app.audioPlayer("PlaybackFinished", function(request, response) { | ||
| + // async response | ||
| + getNextSongFromDB(function(url, token) { |
dblock
Dec 23, 2016
Collaborator
Maybe add something that actually works as long as it's a 1-liner, like fetch some audio clip from some public website?
| @@ -131,7 +133,67 @@ alexa.response = function(session) { | ||
| this.sessionObject.clear(key); | ||
| return this; | ||
| }; | ||
| - | ||
| + this.audioPlayerPlay = function (url, token, playBehavior, offsetInMilliseconds, expectedPreviousToken) { |
dblock
Dec 23, 2016
Collaborator
Which parts are required? Sounds like it's url and playBehavior. So maybe it shouldn't be method that takes 5 parameters but only those 2 and additional options that just get merged? This way you don't need the switch(arguments.length) at all.
fremail
Dec 23, 2016
Contributor
expectedPreviousToken is required only in case of playBehavior is ENQUEUE. Otherwise we don't need it.
Other params are required in all responses: look at the table there
We can try to assign default values to the arguments and make as you wrote, but I don't sure it'd be convenient.
dblock
Dec 24, 2016
Collaborator
I still think this is not future-proof. Every time Amazon adds something to audioplay we'll be throwing more parameters in here. What do you think about leaving this to the user and not being too smart about it?
this.audioPlayerPlay = function (playBehavior, audioItem) {
}
this.audioPlayerPlayStream = function (playBehavior, stream) {
}| + }; | ||
| + this.audioPlayerClearQueue = function (clearBehavior) { | ||
| + var audioPlayerDirective; | ||
| + if (arguments.length === 0) { |
dblock
Dec 23, 2016
Collaborator
There should be a cleaner way to do this one without the big if.
audioPlayerDirective = {}
if () { ... }
self.response....| var requestType = request.type(); | ||
| if (typeof self.pre == "function") { | ||
| - self.pre(request, response, requestType); | ||
| + self.pre(request, response, requestType, context); |
dblock
Dec 23, 2016
Collaborator
I think pre shouldn't take the 4th parameter since request.context is a thing. You can access that inside pre.
fremail
Dec 23, 2016
Contributor
It was added by @wschaeferiii. Really I don't know reasons to add it.
coveralls
commented
Dec 23, 2016
coveralls
commented
Dec 26, 2016
|
Interesting.. when you expand readme file, test coverage decreases. |
dblock
merged commit cb4023f into alexa-js:master Dec 26, 2016
|
Merge, excellent work @fremail and @wschaeferiii. |
This was referenced Dec 26, 2016
|
Btw, just used this in artsy/elderfield#56 and it worked out beautifully. |
fremail commentedDec 23, 2016
Continuing PR #88 I added
app.audioPlayer(event, func)It allows to add an event handler to these AudioPlayer events:
Also this function supports async responses (similar to
intent()).