|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: FramePositioningControl |
|
15 * |
|
16 */ |
|
17 package com.nokia.microedition.media.animation; |
|
18 |
|
19 import javax.microedition.media.MediaException; |
|
20 import javax.microedition.media.Player; |
|
21 |
|
22 import org.eclipse.swt.graphics.ImageData; |
|
23 |
|
24 import com.nokia.microedition.media.control.ControlImpl; |
|
25 import com.nokia.mj.impl.utils.Logger; |
|
26 |
|
27 |
|
28 public class FramePositioningControl extends ControlImpl implements |
|
29 javax.microedition.media.control.FramePositioningControl |
|
30 { |
|
31 private int iTotalNoOfFrames; |
|
32 |
|
33 /** |
|
34 * Constructor of this Control |
|
35 * This constructor should be accessible only in this package, that's why it is being |
|
36 * declared as package protected. |
|
37 */ |
|
38 FramePositioningControl(Player aPlayer) |
|
39 { |
|
40 iPlayer=aPlayer; |
|
41 iTotalNoOfFrames=((AnimationPlayer)iPlayer).getTotalNumberFrames(); |
|
42 } |
|
43 |
|
44 /** |
|
45 * Converts the given frame number to the corresponding media time. |
|
46 * The method only performs the calculations. It does not position the media to the given frame. |
|
47 */ |
|
48 public long mapFrameToTime(int aFrameNumber) |
|
49 { |
|
50 checkState(); |
|
51 int totalNoOfFrames=iTotalNoOfFrames; |
|
52 // if invalid parameter is passed |
|
53 if (aFrameNumber < 0 || aFrameNumber > totalNoOfFrames) |
|
54 return -1; |
|
55 return ((AnimationPlayer)iPlayer).getMediaTimeForFrame(aFrameNumber); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Converts the given media time to the corresponding frame number. |
|
60 * The method only performs the calculations. It does not position the media to the given media time. |
|
61 * The frame returned is the nearest frame that has a media time less than or equal to the given media time. |
|
62 * mapTimeToFrame(0) must not fail and must return the frame number of the first frame. |
|
63 * @param the input media time for the conversion in microseconds. |
|
64 * @return the converted frame number for the given media time. If the conversion fails, -1 is returned. |
|
65 */ |
|
66 public int mapTimeToFrame(long aMediaTime) |
|
67 { |
|
68 checkState(); |
|
69 return ((AnimationPlayer)iPlayer).findFrame(aMediaTime); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Seek to a given video frame. The media time of the Player will be updated to reflect |
|
74 * the new position set. |
|
75 * This method can be called on a stopped or started Player. If the Player is in the Started state, |
|
76 * this method may cause the Player to change states. If that happens, the appropriate transition |
|
77 * events will be posted by the Player when its state changes. |
|
78 * If the given frame number is less than the first or larger than the last frame number in the media, |
|
79 * seek will jump to either the first or the last frame respectively. |
|
80 * @param aFrameNumber the frame to seek to. |
|
81 * @return the actual number of frames skipped. |
|
82 */ |
|
83 public int seek(int aFrameNumber) |
|
84 { |
|
85 final String DEBUG_STR = "FramePositionControl::seek"; |
|
86 // Check the state of the player, it shouldn't be in closed state |
|
87 Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR + "+"); |
|
88 checkState(); |
|
89 int frameNumber = aFrameNumber; |
|
90 int totalNoOfFrames = iTotalNoOfFrames; |
|
91 if (aFrameNumber < 0) |
|
92 { |
|
93 frameNumber = 0; |
|
94 } |
|
95 else if (aFrameNumber >= totalNoOfFrames) |
|
96 { |
|
97 frameNumber = totalNoOfFrames-1; |
|
98 } |
|
99 long mediaTime = ((AnimationPlayer)iPlayer).getMediaTimeForFrame(frameNumber); |
|
100 try |
|
101 { |
|
102 iPlayer.setMediaTime(mediaTime); |
|
103 } |
|
104 catch (MediaException e) |
|
105 { |
|
106 // Just ignore the exception |
|
107 e.printStackTrace(); |
|
108 } |
|
109 Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR + "- seeked to "+frameNumber); |
|
110 return frameNumber; |
|
111 } |
|
112 |
|
113 /** |
|
114 * Skip a given number of frames from the current position. The media time of the Player will be |
|
115 * updated to reflect the new position set. |
|
116 * This method can be called on a stopped or started Player. If the Player is in the Started state, |
|
117 * the current position is changing. Hence, the frame actually skipped to will not be exact. |
|
118 * If the Player is in the Started state, this method may cause the Player to change states. |
|
119 * If that happens, the appropriate transition events will be posted. |
|
120 * If the given framesToSkip will cause the position to extend beyond the first or last frame, |
|
121 * skip will jump to the first or last frame respectively. |
|
122 * @param framesToSkip - the number of frames to skip from the current position. |
|
123 * If framesToSkip is positive, it will seek forward by framesToSkip number of frames. |
|
124 * If framesToSkip is negative, it will seek backward by framesToSkip number of frames. |
|
125 * e.g. skip(-1) will seek backward one frame. |
|
126 * |
|
127 * @return the actual number of frames skipped. |
|
128 */ |
|
129 public int skip(int aFramesToSkip) |
|
130 { |
|
131 // final String DEBUG_STR = "FramePositionControl::skip"; |
|
132 //Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR+"+"); |
|
133 // check the state of the player, if it closed throw exception |
|
134 checkState(); |
|
135 //int frameNumberToJump; |
|
136 int currentFrameIndex=((AnimationPlayer) iPlayer).getiFrameIndex(); |
|
137 //If user provides the argument as Integer.max, adding any positive value to it will make it negative |
|
138 //Since a player can't be skipped more than the total number of frames, here we are making it restrictive. |
|
139 if (aFramesToSkip > iTotalNoOfFrames) |
|
140 { |
|
141 aFramesToSkip = iTotalNoOfFrames; |
|
142 } |
|
143 // we are going to utilize the seek function here |
|
144 // just get the current frame index from player and |
|
145 // add it to the number of frame to skip |
|
146 int seeked=seek(currentFrameIndex + aFramesToSkip); |
|
147 int numberOfFrameSkipped=seeked-currentFrameIndex; |
|
148 return numberOfFrameSkipped; |
|
149 } |
|
150 |
|
151 } |