javascriptyahoo-widgets

Yahoo! Connected TV playback events


I'm trying to subscribe to onTimeIndexChanged mediaplayer event. It works fine, as long as view that I'm subscribing in stays focused. When I press back button, or widgets button, my view gets blurred and it no longer receives events.

Is it possible for this subscription to persist through switching views? Are there widget-wide subscriptions?

I am trying to find out if it is possible to count time of playback client-side.


Solution

  • This was also answered on the Yahoo! Connected TV forums at: http://developer.yahoo.net/forum/?showtopic=7383 Yes, you're on the right track. The media player is a singleton and as such isn't bound to any particular view. You can define a listener that subscribes to it in the widget's global execution context. This way you can still receive and handle events that happen when a view is gc'd, and you can still receive those events.

    We put these global subscriptions in init.js so they're centrally-located (best practice).

     
    EventHandlers.handlerPlayerEvent.subscribeTo(KONtx.mediaplayer, ['onStateChange', 'onTimeIndexChanged'], EventHandlers);
    

    Then, in Javascript/core/EventHandlers.js:

    
    var EventHandlers = {

        //snipped for brevity;
    
        handlerPlayerEvent: function(event) {
                switch(event.type) {
                        case 'onStateChange':
                                switch(event.payload.newState) {
                                        case KONtx.mediaplayer.constants.states.PLAY:
                                                if(!this._snippetAdded) {
                                                        KONtx.application.addViewConfig({ id: 'snippet-nowplaying',
    

    viewClass: VideoNowPlayingSnippetView }); this._snippetAdded = true; } break; case KONtx.mediaplayer.constants.states.UNKNOWN: case KONtx.mediaplayer.constants.states.ERROR: case KONtx.mediaplayer.constants.states.STOP: case KONtx.mediaplayer.constants.states.EOF: KONtx.application.removeView('snippet-nowplaying'); this._snippetAdded = false; break; } break;

                        case 'onTimeIndexChanged':
                                //do something interesting;
                                break;
                }
        }
    

    };

    I should also add that if you're subscribing to events in a view that it's very important to also unsubscribe from them in the view's hideView listener. This can prevent memory leaks and other undesirable behavior.