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

[Question] Anybody found an event to detect when a track has ended playing? #71

Closed
julianCast opened this issue Oct 12, 2020 · 3 comments

Comments

@julianCast
Copy link

Not an issue, but a question:

The only post I could find is this one https://stackoverflow.com/questions/19645972/chromecast-sdk-android-is-there-a-way-to-check-whether-the-media-playing-on but I wonder if any of you have used a different approach, it looks much for what it is.

The documentation has https://developers.google.com/cast/docs/reference/chrome/chrome.cast.media.Media#addUpdateListener but does not provide such a action.

Thanks!

@Lindsay-Needs-Sleep
Copy link
Collaborator

You are on the right track with media.addUpdateListener.

You can see a bunch of example code in the tests directory for this plugin (eg. tests_auto.js).
There is extra stuff going on there, but you might be able to figure out what you need for future things.

For detecting when a track has ended, this is what I used (simplified from the tests_auto.js):

media.addUpdateListener(function listener (isAlive) {
    if (media.playerState === chrome.cast.media.PlayerState.IDLE) {
        if (media.idleReason === chrome.cast.media.IdleReason.FINISHED) {
           // The current media has finished playing by reaching the end
        }
        if (media.idleReason === chrome.cast.media.IdleReason.CANCELLED) {
           // The current media has been stopped (media.stop)
        }
        if (media.idleReason === chrome.cast.media.IdleReason.INTERRUPTED) {
           // The current media has ended because new media has been loaded (media.queueJumpToItem)
        }
    }
});

Here is some of the relevant documentation:
playerState
idleReason

@julianCast
Copy link
Author

That is great, thank you again.
I was wrongly only using addUpdateListener function from the session, not the media.

@Lindsay-Needs-Sleep
Copy link
Collaborator

Ahh, yep, easy mistake to make!
np! 👍

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

No branches or pull requests

2 participants