TOC Prev Next
This appendix describes an implementation of ControllerListener, ControllerAdapter
, that can be easily extended to respond to particular events.
Implementing ControllerAdapter
ControllerAdapter is an event adapter that recieves ControllerEvents and dispatches
them to an appropriate stub-method. Classes use this adapter by extending
it and replacing only the message handlers that they are interested in.
import javax.media.*;
public void cachingControl(CachingControlEvent e) {}
public void controllerClosed(ControllerClosedEvent e) {}
public void controllerError(ControllerErrorEvent e) {}
public void connectionError(ConnectionErrorEvent e) {}
public void internalError(InternalErrorEvent e) {}
public void resourceUnavailable(ResourceUnavailableEvent
e) {}
public void durationUpdate(DurationUpdateEvent e) {}
public void mediaTimeSet(MediaTimeSetEvent e) {}
public void rateChange(RateChangeEvent e) {}
public void stopTimeChange(StopTimeChangeEvent e) {}
public void transition(TransitionEvent e) {}
public void prefetchComplete(PrefetchCompleteEvent e) {}
public void realizeComplete(RealizeCompleteEvent e) {}
public void start(StartEvent e) {}
public void stop(StopEvent e) {}
public void dataStarved(DataStarvedEvent e) {}
public void deallocate(DeallocateEvent e) {}
public void endOfMedia(EndOfMediaEvent e) {}
public void restarting(RestartingEvent e) {}
public void stopAtTime(StopAtTimeEvent e) {}
public void stopByRequest(StopByRequestEvent e) {}
/**
* Main dispatching function. Subclasses should not need to
* override this method, but instead subclass only
* the individual event methods listed above that they need
*/
public void controllerUpdate(ControllerEvent e) {
if (e instanceof CachingControlEvent) {
cachingControl((CachingControlEvent)e);
} else if ( e instanceof ControllerClosedEvent) {
controllerClosed((ControllerClosedEvent)e);
if (e instanceof ControllerErrorEvent) {
controllerError((ControllerErrorEvent)e);
if (e instanceof DataLostErrorEvent) {
connectionError((ConnectionErrorEvent)e);
} else if (e instanceof InternalErrorEvent) {
internalError((InternalErrorEvent)e);
} else if (e instanceof ResourceUnavailableEvent) {
resourceUnavailable((ResourceUnavailableEvent)e);
}
}
} else if (e instanceof DurationUpdateEvent) {
durationUpdate((DurationUpdateEvent)e);
} else if (e instanceof MediaTimeSetEvent) {
mediaTimeSet((MediaTimeSetEvent)e);
} else if (e instanceof RateChangeEvent) {
rateChange((RateChangeEvent)e);
} else if (e instanceof StopTimeChangeEvent) {
stopTimeChange((StopTimeChangeEvent)e);
} else if (e instanceof TransitionEvent) {
transition((TransitionEvent)e);
if (e instanceof PrefetchCompleteEvent) {
prefetchComplete((PrefetchCompleteEvent)e);
} else if (e instanceof RealizeCompleteEvent) {
realizeComplete((RealizeCompleteEvent)e);
} else if (e instanceof StartEvent) {
start((StartEvent)e);
} else if (e instanceof StopEvent) {
stop((StopEvent)e);
if(e instanceof DataStarvedEvent) {
dataStarved((DataStarvedEvent)e);
} else if (e instanceof DeallocateEvent) {
deallocate((DeallocateEvent)e);
} else if (e instanceof EndOfMediaEvent) {
endOfMedia((EndOfMediaEvent)e);
} else if (e instanceof RestartingEvent) {
restarting((RestartingEvent)e);
} else if (e instanceof StopAtTimeEvent) {
stopAtTime((StopAtTimeEvent)e);
} else if (e instanceof StopByRequestEvent) {
stopByRequest((StopByRequestEvent)e);
}
}
}
}
}
|
Using ControllerAdapter
To implement the ControllerListener interface using a ControllerAdapter,
you need to:
- Subclass
ControllerAdapter and override the event methods for the events
that you're interested in.
- Register your
ControllerAdapter class as a listener for a particular
Controller by calling addControllerListener.
When a Controller posts an event, it calls controllerUpdate on each registered
listener. ControllerAdapter automatically dispatches the event to the
appropriate event method, filtering out the events that you're not interested in.
For example, the following code extends a ControllerAdapter with a JDK 1.1
anonymous inner-class to create a self-contained Player that is automatically
reset to the beginning of the media and deallocated when the Player reaches the
end of the media:
player.addControllerListener(new ControllerAdapter() {
public void endOfMedia(EndOfMediaEvent e) {
Controller controller = e.getSource();
controller.stop();
controller.setMediaTime(0);
controller.deallocate();
}
}
|
If you register a single ControllerAdapter as a listener for multiple Players, in
your event method implementations you need to determine which Player generated
the event. Controller events come "stamped" with a reference to their
source that you can access by calling getSource.
TOC Prev Next
|