-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AudioPlayer functionality (Continue PR #88) #92
Conversation
…udio using response object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a link to audio player support on some amazon site to explain what this is somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the following events ...
And space things out a bit, add an empty line above and below.
* PlaybackNearlyFinished | ||
* PlaybackFailed | ||
|
||
See example further below. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say "The following example [explain what this does]."
}); | ||
app.audioPlayer("PlaybackFinished", function(request, response) { | ||
// async response | ||
getNextSongFromDB(function(url, token) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add something that actually works as long as it's a 1-liner, like fetch some audio clip from some public website?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have ideas what it could be
@@ -131,7 +133,67 @@ alexa.response = function(session) { | |||
this.sessionObject.clear(key); | |||
return this; | |||
}; | |||
|
|||
this.audioPlayerPlay = function (url, token, playBehavior, offsetInMilliseconds, expectedPreviousToken) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a cleaner way to do this one without the big if.
audioPlayerDirective = {}
if () { ... }
self.response....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I'll rewrite this function
var requestType = request.type(); | ||
if (typeof self.pre == "function") { | ||
self.pre(request, response, requestType); | ||
self.pre(request, response, requestType, context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think pre
shouldn't take the 4th parameter since request.context
is a thing. You can access that inside pre
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was added by @wschaeferiii. Really I don't know reasons to add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets remove code that we don't know why it's there :)
Remove fourth argument of app.pre() function Update README
Add response.audioPlayerPlayStream function as a helper to response.audioPlayerPlay function Update README
Interesting.. when you expand readme file, test coverage decreases. |
Merge, excellent work @fremail and @wschaeferiii. |
Btw, just used this in artsy/elderfield#56 and it worked out beautifully. |
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()
).