Skip to content
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

Merged
merged 12 commits into from
Dec 26, 2016

Conversation

fremail
Copy link
Contributor

@fremail fremail commented Dec 23, 2016

Continuing PR #88 I added app.audioPlayer(event, func)
It allows to add an event handler to these AudioPlayer events:

  • PlaybackStarted
  • PlaybackFinished
  • PlaybackStopped
  • PlaybackNearlyFinished
  • PlaybackFailed

Also this function supports async responses (similar to intent()).

@coveralls
Copy link

Coverage Status

Coverage increased (+1.9%) to 87.153% when pulling d624ee2 on fremail:continue-88 into e2b8c6c on matt-kruse:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+1.9%) to 87.153% when pulling 5faa7ad on fremail:continue-88 into e2b8c6c on matt-kruse:master.

Copy link
Collaborator

@dblock dblock left a 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.
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

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:
Copy link
Collaborator

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.
Copy link
Collaborator

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) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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) {
Copy link
Collaborator

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....

Copy link
Contributor Author

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);
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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
@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 85.512% when pulling a42d46c on fremail:continue-88 into e2b8c6c on matt-kruse:master.

Add response.audioPlayerPlayStream function as a helper to response.audioPlayerPlay function
Update README
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 85.091% when pulling acd32a8 on fremail:continue-88 into e2b8c6c on matt-kruse:master.

@fremail
Copy link
Contributor Author

fremail commented Dec 26, 2016

Interesting.. when you expand readme file, test coverage decreases.

@dblock dblock merged commit cb4023f into alexa-js:master Dec 26, 2016
@dblock
Copy link
Collaborator

dblock commented Dec 26, 2016

Merge, excellent work @fremail and @wschaeferiii.

@dblock
Copy link
Collaborator

dblock commented Feb 7, 2017

Btw, just used this in artsy/elderfield#56 and it worked out beautifully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants