javax.media Interface Clock All Known Subinterfaces: Controller, Player, Processor public interface Clock The Clock interface is implemented by objects that support the Java Media time model. For example, this interface might be implemented by an object that decodes and renders MPEG movies. Clock and TimeBase A Clock contains a TimeBase that provides a source of time, much like a crystal oscillator. The only information that a TimeBase provides is its current time; it does not provide any methods for influencing how time is kept. A Clock defines a transformation on the time that its TimeBase keeps, typically marking time for a particular media stream. The time that a Clock keeps is referred to as the media time. Clock Transform The transformation that a Clock defines on a TimeBase is defined by three parameters: rate, media start-time (mst), and time-base start-time (tbst). Given a time-base time (tbt), the media time (mt) can be calculated using the following transformation: mt = mst + (tbt - tbst)*rate The rate is simply a scale factor that is applied to the TimeBase. For example, a rate of 2.0 indicates that the Clock will run at twice the rate of its TimeBase. Similarly, a negative rate indicates that the Clock runs in the opposite direction of its TimeBase. The time-base start-time and the media start-time define a common point in time at which the Clock and the TimeBase are synchronized. Default Time Base A Clock has a default TimeBase. For many objects that support the Clock interface, the default TimeBase is the system TimeBase. The system TimeBase can be obtained from Manager through the getSystemTimeBase method. Some Clocks have a TimeBase other than the system TimeBase. For example, an audio renderer that implements the Clock interface might have a TimeBase that represents a hardware clock. Using a Clock You can get the TimeBase associated with a Clock by calling the getTimeBase method. To change the TimeBase that a Clock uses, you call the setTimeBase method. These get and set methods can be used together to synchronize different Clocks to the same TimeBase. For example, an application might want to force a video renderer to sync to the TimeBase of an audio renderer. To do this, the application would call getTimeBase on the audio renderer and then use the value returned to call setTimeBase on the video renderer. This would ensure that the two rendering objects use the same source of time. You can reset a Clock to use its default TimeBase by calling setTimeBase(null). Some Clocks are incapable of using another TimeBase. If this is the case, an IncompatibleTimeBaseException is thrown when setTimeBase is called. Clock also provides methods for getting and setting a Clock's media time and rate: getMediaTime and setMediaTime getRate and setRate Starting a Clock Until a Clock's TimeBase transformation takes effect, the Clock is in the Stopped state. Once all three transformation parameters (media start-time, time-base start-time, and rate) have been provided to the Clock, it enters the Started state. To start a Clock, syncStart is called with the time-base start-time as an argument. The new media start-time is taken as the current media time, and the current rate defines the Clock's rate parameter. When syncStart is called, the Clock and its TimeBase are locked in sync and the Clock is considered to be in the Started state. When a Clock is stopped and then restarted (using syncStart), the media start-time for the restarted Clock is the current media time. The syncStart method is often used to synchronize two Clocks that share the same TimeBase. When the time-base start-time and rate of each clock are set to the same values and each Clock is set with the appropriate media start-time, the two Clocks will run in sync. When syncStart is called with a new time-base start-time, the synchronization with the media time doesn't occur until the TimeBase reaches the time-base start-time. The getMediaTime method returns the untransformed media time until the TimeBase reaches the time-base start-time. The getSyncTime method behaves slightly differently. Once syncStart is invoked, getSyncTime always reports the transformed time-base time, whether or not the time-base start-time has been reached. You can use getSyncTime to determine how much time remains before the time-base start-time is reached. When the time-base start-time is reached, both getMediaTime and getSyncTime return the same value. Objects that implement the Clock interface can provide more convenient start methods than syncStart. For example, Player defines start, which should be used instead of syncStart to start a Player. Stopping a Clock A Stopped Clock is no longer synchronized to its TimeBase. When a Clock is Stopped, its media time no longer moves in rate-adjusted synchronization with the time-base time provided by its TimeBase. There are two ways to explicitly stop a Clock: you can invoke stop or set a media stop-time. When stop is invoked, synchronization with the TimeBase immediately stops. When a media stop-time is set, synchronization stops when the media stop-time passes. A Clock's rate affects how its media stop-time is interpreted. If its rate is positive, the Clock stops when the media time becomes greater than or equal to the stop time. If its rate is negative, the Clock stops when the media time becomes less than or equal to the stop time. If the stop-time is set to a value that the Clock has already passed, the Clock immediately stops. Once a stop-time is set, it remains in effect until it is changed or cleared. To clear a stop-time, call setStopTime with Clock.RESET. A Clock's stop-time is cleared automatically when it stops. If no stop-time is ever set or if the stop-time is cleared, the only way to stop the Clock is to call the stop method. Clock State Conceptually, a Clock is always in one of two states: Started or Stopped. A Clock enters the Started state after syncStart has been called and the Clock is mapped to its TimeBase. A Clock returns to the Stopped state immediately when the stop method is called or the media time passes the stop time. Certain methods can only be invoked when the Clock is in a particular state. If the Clock is in the wrong state when one of these methods is called, an error or exception is thrown. Methods Restricted to Started Clocks The mapToTimeBase method can only be called on a Clock in the Started state. If it is invoked on a Stopped Clock, a ClockStoppedException is thrown. This is because the Clock is not synchronized to a TimeBase when it is Stopped. Methods Restricted to Stopped Clocks The following methods can only be called on a Clock in the Stopped state. If invoked on a Started Clock, these methods throw a ClockStartedError. syncStart setTimeBase setMediaTime setRate Resetting the rate, the media time, the time base, or the time-base start-time implies a complete remapping between the Clock and its TimeBase and is not allowed on a Started Clock. Methods with Additional Restrictions A race condition occurs if a new media stop-time is set when a Clock is already approaching a previously set media stop-time. In this situation, it impossible to guarantee when the Clock will stop. To prevent this race condition, setStopTime can only be set once on a Started Clock. A StopTimeSetError is thrown if setStopTime is called and the media stop-time has already been set. There are no restrictions on calling setStopTime on a Stopped Clock; the stop time can always be reset if the Clock is Stopped. See Also: TimeBase, Player Field Summary static Time RESET Returned by getStopTime if the stop-time is unset. Method Summary long getMediaNanoseconds() Gets this Clock's current media time in nanoseconds. Time getMediaTime() Gets this Clock's current media time. float getRate() Gets the current temporal scale factor. Time getStopTime() Gets the last value successfully set by setStopTime. Time getSyncTime() Gets the current media time or the time until this Clock will synchronize to its TimeBase. TimeBase getTimeBase() Gets the TimeBase that this Clock is using. Time mapToTimeBase(Time t) Gets the TimeBase time corresponding to the specified media time. void setMediaTime(Time now) Sets the Clock's media time. float setRate(float factor) Sets the temporal scale factor. void setStopTime(Time stopTime) Sets the media time at which you want the Clock to stop. void setTimeBase(TimeBase master) Sets the TimeBase for this Clock. void stop() Stops the Clock. void syncStart(Time at) Synchronizes the current media time to the specified time-base time and start the Clock. Field Detail RESET public static final Time RESET Returned by getStopTime if the stop-time is unset. Method Detail setTimeBase public void setTimeBase(TimeBase master) throws IncompatibleTimeBaseException Sets the TimeBase for this Clock. This method can only be called on a Stopped Clock. A ClockStartedError is thrown if setTimeBase is called on a Started Clock. A Clock has a default TimeBase that is determined by the implementation. To reset a Clock to its default TimeBase, call setTimeBase(null). Parameters: master - The new TimeBase or null to reset the Clock to its default TimeBase. Throws: IncompatibleTimeBaseException - Thrown if the Clock can't use the specified TimeBase. syncStart public void syncStart(Time at) Synchronizes the current media time to the specified time-base time and start the Clock. The syncStart method sets the time-base start-time, and puts the Clock in the Started state. This method can only be called on a Stopped Clock. A ClockStartedError is thrown if setTimeBase is called on a Started Clock. Parameters: at - The time-base time to equate with the current media time. stop public void stop() Stops the Clock. Calling stop releases the Clock from synchronization with the TimeBase. After this request is issued, the Clock is in the Stopped state. If stop is called on a Stopped Clock, the request is ignored. setStopTime public void setStopTime(Time stopTime) Sets the media time at which you want the Clock to stop. The Clock will stop when its media time passes the stop-time. To clear the stop time, set it to: Clock.RESET. You can always call setStopTime on a Stopped Clock. On a Started Clock, the stop-time can only be set once. A StopTimeSetError is thrown if setStopTime is called and the media stop-time has already been set. Parameters: stopTime - The time at which you want the Clock to stop, in media time. getStopTime public Time getStopTime() Gets the last value successfully set by setStopTime. Returns the constant Clock.RESET if no stop time is set. (Clock.RESET is the default stop time.) Returns: The current stop time. setMediaTime public void setMediaTime(Time now) Sets the Clock's media time. This method can only be called on a Stopped Clock. A ClockStartedError is thrown if setMediaTime is called on a Started Clock. Parameters: now - The new media time. getMediaTime public Time getMediaTime() Gets this Clock's current media time. A Started Clock's media time is based on its TimeBase and rate, as described in Starting a Clock. Returns: The current media time. getMediaNanoseconds public long getMediaNanoseconds() Gets this Clock's current media time in nanoseconds. Returns: The current media time in nanoseconds. getSyncTime public Time getSyncTime() Gets the current media time or the time until this Clock will synchronize to its TimeBase. The getSyncTime method is used by Players and advanced applet writers to synchronize Clocks. Like getMediaTime, this method returns the Clock's current media time, which is based on its TimeBase and rate. However, when syncStart is used to start the Clock, getSyncTime performs a countdown to the time-base start-time, returning the time remaining until the time-base start-time. Once the TimeBase reaches the time-base start-time, getSyncTime and getMediaTime will return the same value. getTimeBase public TimeBase getTimeBase() Gets the TimeBase that this Clock is using. mapToTimeBase public Time mapToTimeBase(Time t) throws ClockStoppedException Gets the TimeBase time corresponding to the specified media time. Parameters: t - The media time to map from. Returns: The time-base time in media-time coordinates. Throws: ClockStoppedException - Thrown if mapToTimeBase is called on a Stopped Clock. getRate public float getRate() Gets the current temporal scale factor. The scale factor defines the relationship between the Clock's media time and its TimeBase. For example, a rate of 2.0 indicates that media time will pass twice as fast as the TimeBase time once the Clock starts. Similarly, a negative rate indicates that the Clock runs in the opposite direction of its TimeBase. All Clocks are guaranteed to support a rate of 1.0, the default rate. Clocks are not required to support any other rate. setRate public float setRate(float factor) Sets the temporal scale factor. The argument suggests the scale factor to use. The setRate method returns the actual rate set by the Clock. Clocks should set their rate as close to the requested value as possible, but are not required to set the rate to the exact value of any argument other than 1.0. A Clock is only guaranteed to set its rate exactly to 1.0. You can only call this method on a Stopped Clock. A ClockStartedError is thrown if setRate is called on a Started Clock. Parameters: factor - The temporal scale factor (rate) to set. Returns: The actual rate set.
javax.media 接口时钟 所有已知子接口： 控制器，播放器，处理器 公共接口时钟 该Clock接口由支持Java Media时间模型的对象实现。例如，该接口可以由解码和呈现MPEG电影的对象实现。 时钟和时基 A Clock包含TimeBase提供时间源的a，就像晶体振荡器一样。提供的唯一信息TimeBase是其当前时间; 它没有提供任何影响时间的方法。A Clock定义了其TimeBase保留时间的转换，通常标记特定媒体流的时间。Clock保持的时间称为媒体时间。 时钟变换 a Clock定义的转换TimeBase 由三个参数定义：速率，媒体启动时间（mst）和 时基开始时间（tbst）。给定时基时间（tbt），可以使用以下转换计算媒体时间（mt）： mt = mst + (tbt - tbst)*rate 速率只是应用于的比例因子TimeBase。例如，速率为2.0表示Clock 将以其速率的两倍运行TimeBase。同样，负利率表明其Clock运行方向与其相反TimeBase。 的时基开始时间以及 媒体起动时限定在公共时间点在其中Clock和TimeBase是同步的。 默认时基 A Clock有默认值TimeBase。对于支持该Clock接口的许多对象，默认 TimeBase为系统TimeBase。该系统TimeBase可以Manager通过该 getSystemTimeBase方法获得。 有些Clocks人TimeBase不是系统TimeBase。例如，实现Clock 接口的音频渲染器可能具有TimeBase表示硬件时钟的音频渲染器。 使用时钟 您可以通过调用方法获得TimeBase与a相关联。要改变 一个用途，可以调用方法。这些get和set方法可以一起使用，以同步不同，以相同的。 ClockgetTimeBaseTimeBaseClocksetTimeBaseClocksTimeBase 例如，应用程序可能希望强制视频渲染器同步到TimeBase音频渲染器。为此，应用程序将调用getTimeBase音频渲染器，然后使用返回的值调用setTimeBase视频渲染器。这将确保两个渲染对象使用相同的时间源。您可以通过调用重置a Clock以使用其默认值。 TimeBasesetTimeBase(null) 有些Clocks人无法使用其他人TimeBase。如果是这种情况，IncompatibleTimeBaseException则在setTimeBase 调用时抛出a 。 Clock还提供了获取和设置Clock's 媒体时间和速率的方法： getMediaTime 和 setMediaTime getRate 和 setRate 开始时钟 在Clock's TimeBase转换生效之前，Clock它处于Stopped状态。一旦提供了所有三个转换参数（介质开始时间， 时基开始时间和速率）Clock，它就进入启动状态。 要启动a Clock，syncStart以时基开始时间作为参数调用。新媒体开始时间作为当前 媒体时间，当前速率定义Clock's速率参数。当syncStart被调用时，Clock其TimeBase被锁定在同步和Clock被认为是在启动状态。 当Clock停止并重新启动（使用syncStart）时，媒体开始时间 为重新启动Clock是当前媒体时间。该syncStart方法通常用于同步两个Clocks共享相同的方法TimeBase。当每个时钟的时基开始时间和速率设置为相同的值并且每个时钟都Clock使用适当的媒体启动时间设置时，两者Clocks将同步运行。 当syncStart使用新的时基启动时调用时，在 到达时基开始时间之前不会发生与媒体时间的同步。该方法返回未转换的媒体时间，直到 达到时基开始时间。 TimeBasegetMediaTimeTimeBase 该getSyncTime方法的行为略有不同。一旦syncStart被调用，getSyncTime 始终报告转换的时基时间，无论是否已达到时基开始时间。您可以使用它getSyncTime来确定在达到时基开始时间之前剩余的时间。当时基的启动时间到达，都 getMediaTime和getSyncTime 返回相同的值。 实现Clock接口的对象可以提供比它更方便的启动方法syncStart。例如，Playerdefine start，应该用来代替syncStart 启动a Player。 停止时钟 已停止 Clock不再与其同步TimeBase。当Clock被停止，其媒体时间 不再与所述速率调整的同步移动时基时间由其提供TimeBase。 有两种方法可以显式停止Clock：您可以调用 stop或设置媒体停止时间。当stop被调用时，与同步 TimeBase立即停止。设置媒体停止时间时，媒体停止时间过后同步停止。 一个Clock's速率会影响其如何媒体停止时间 解释。如果其速率为正，则Clock 当媒体时间大于或等于停止时间时停止。如果其速率为负，则Clock当媒体时间小于或等于停止时间时停止。 如果停止时间设置为Clock 已经过的值，则Clock立即停止。 一旦设置了停止时间，它将一直有效，直到它被更改或清除。要清除停止时间，通话setStopTime 用Clock.RESET。一个Clock's当它停止停止时自动清零。 如果没有设置停止时间或清除停止时间，则停止的唯一方法Clock是调用stop方法。 时钟状态 从概念上讲，a Clock始终处于以下两种状态之一：已启动或已停止。A 在被调用后Clock进入Started状态 syncStart并Clock 映射到它TimeBase。当调用方法或 媒体时间超过停止时间时，A 立即Clock返回到Stopped状态。 stop 某些方法只能Clock在处于特定状态时调用。如果在Clock调用其中一个方法时处于错误状态，则抛出错误或异常。 限制启动时钟的方法 该mapToTimeBase方法只能Clock在启动状态下调用 。如果在Stopped 上调用它Clock，ClockStoppedException则抛出a。这是因为它与Stopt时Clock不同步。 TimeBase 限制停止时钟的方法 只能Clock在Stopped状态下调用以下方法 。如果在Started 上调用Clock，这些方法会抛出一个ClockStartedError。 syncStart setTimeBase setMediaTime setRate 重置速率，媒体时间，时基或 时基开始时间意味着在它Clock和它之间完全重新映射，并且 TimeBase不允许在已启动时 Clock。 附加限制的方法 如果在a 已经接近先前设置的媒体停止时间时设置新的媒体停止时间，则发生竞争条件。在这种情况下，无法保证什么时候 会停止。要防止此竞争条件，只能在已启动时设置一次。如果 被调用并且已经设置 了媒体停止时间，则抛出A.ClockClocksetStopTime ClockStopTimeSetErrorsetStopTime 有关于调用没有限制setStopTime上 停止 Clock ; 如果Clock已停止，则始终可以重置停止时间。