0

Subscribing to mediaplayer events

I'm trying to subscribe to onTimeIndexChanged KONtx.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 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.

by
2 Replies
  • QUOTE (Bartosz Borkowski @ Oct 19 2010, 06:42 AM) <{POST_SNAPBACK}>
    I'm trying to subscribe to onTimeIndexChanged KONtx.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 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.

    Hi Bartosz,
    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).
    CODE
    EventHandlers.handlerPlayerEvent.subscribeTo(KONtx.mediaplayer, ['onStateChange', 'onTimeIndexChanged'], EventHandlers);


    Then, in Javascript/core/EventHandlers.js:
    CODE
    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 hope that helps.

    - Ben
    0
  • 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.

    - Ben
    0

Recent Posts

in Design / Interaction - Yahoo! TV Widgets