javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/animation/FramePositioningControl.java
changeset 48 e0d6e9bd3ca7
parent 35 85266cc22c7f
equal deleted inserted replaced
47:f40128debb5d 48:e0d6e9bd3ca7
    26 
    26 
    27 
    27 
    28 public class FramePositioningControl extends ControlImpl implements
    28 public class FramePositioningControl extends ControlImpl implements
    29         javax.microedition.media.control.FramePositioningControl
    29         javax.microedition.media.control.FramePositioningControl
    30 {
    30 {
    31 
    31     private int iTotalNoOfFrames;
    32     private ImageData[] iImagedata;
       
    33 
    32 
    34     /**
    33     /**
    35      * Constructor of this Control
    34      * Constructor of this Control
    36      * This constructor should be accessible only in this package, that's why it is being
    35      * This constructor should be accessible only in this package, that's why it is being
    37      * declared as package protected.
    36      * declared as package protected.
    38      */
    37      */
    39     FramePositioningControl(Player aPlayer)
    38     FramePositioningControl(Player aPlayer)
    40     {
    39     {
    41         iPlayer=aPlayer;
    40         iPlayer=aPlayer;
    42         iImagedata=((AnimationPlayer)iPlayer).getImageData();
    41         iTotalNoOfFrames=((AnimationPlayer)iPlayer).getTotalNumberFrames();
    43     }
    42     }
    44 
    43 
    45     /**
    44     /**
    46      * Converts the given frame number to the corresponding media time.
    45      * Converts the given frame number to the corresponding media time.
    47      * The method only performs the calculations. It does not position the media to the given frame.
    46      * The method only performs the calculations. It does not position the media to the given frame.
    48      */
    47      */
    49     public long mapFrameToTime(int aFrameNumber)
    48     public long mapFrameToTime(int aFrameNumber)
    50     {
    49     {
    51         checkState();
    50         checkState();
    52         long time=0;
    51         int totalNoOfFrames=iTotalNoOfFrames;
    53         int totalNoOfFrames=iImagedata.length;
       
    54         // if invalid parameter is passed
    52         // if invalid parameter is passed
    55         if (aFrameNumber<0 || aFrameNumber>totalNoOfFrames)
    53         if (aFrameNumber < 0 || aFrameNumber > totalNoOfFrames)
    56             return -1;
    54             return -1;
    57         for (int i=0; i<totalNoOfFrames; i++)
    55         return ((AnimationPlayer)iPlayer).getMediaTimeForFrame(aFrameNumber);
    58         {
       
    59             if (i==aFrameNumber)
       
    60                 break;
       
    61             time+=iImagedata[i].delayTime;
       
    62         }
       
    63         return time*10000;
       
    64     }
    56     }
    65 
    57 
    66     /**
    58     /**
    67      * Converts the given media time to the corresponding frame number.
    59      * Converts the given media time to the corresponding frame number.
    68      * The method only performs the calculations. It does not position the media to the given media time.
    60      * The method only performs the calculations. It does not position the media to the given media time.
    72      * @return the converted frame number for the given media time. If the conversion fails, -1 is returned.
    64      * @return the converted frame number for the given media time. If the conversion fails, -1 is returned.
    73      */
    65      */
    74     public int mapTimeToFrame(long aMediaTime)
    66     public int mapTimeToFrame(long aMediaTime)
    75     {
    67     {
    76         checkState();
    68         checkState();
    77         int frameNumber=-1;
    69         return ((AnimationPlayer)iPlayer).findFrame(aMediaTime);
    78         int totalNoOfFrames=iImagedata.length;
       
    79         long time=0;
       
    80         for (int i=0; i<totalNoOfFrames; i++)
       
    81         {
       
    82             if (time > aMediaTime)
       
    83             {
       
    84                 frameNumber=i-1;
       
    85                 break;
       
    86             }
       
    87             time+=iImagedata[i].delayTime*10000;
       
    88         }
       
    89         return frameNumber;
       
    90     }
    70     }
    91 
    71 
    92     /**
    72     /**
    93      * Seek to a given video frame. The media time of the Player will be updated to reflect
    73      * Seek to a given video frame. The media time of the Player will be updated to reflect
    94      * the new position set.
    74      * the new position set.
   105         final String DEBUG_STR = "FramePositionControl::seek";
    85         final String DEBUG_STR = "FramePositionControl::seek";
   106         // Check the state of the player, it shouldn't be in closed state
    86         // Check the state of the player, it shouldn't be in closed state
   107         Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR + "+");
    87         Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR + "+");
   108         checkState();
    88         checkState();
   109         int frameNumber = aFrameNumber;
    89         int frameNumber = aFrameNumber;
   110         int totalNoOfFrames = iImagedata.length;
    90         int totalNoOfFrames = iTotalNoOfFrames;
   111         if (aFrameNumber < 0)
    91         if (aFrameNumber < 0)
   112         {
    92         {
   113             frameNumber = 0;
    93             frameNumber = 0;
   114         }
    94         }
   115         else
    95         else if (aFrameNumber >= totalNoOfFrames)
   116         {
    96         {
   117             if (aFrameNumber > totalNoOfFrames)
    97             frameNumber = totalNoOfFrames-1;
   118             {
       
   119                 frameNumber = totalNoOfFrames;
       
   120             }
       
   121         }
    98         }
   122         long mediaTime = mapFrameToTime(frameNumber);
    99         long mediaTime = ((AnimationPlayer)iPlayer).getMediaTimeForFrame(frameNumber);
   123         //if the frame number is equal to total number of frames, we will seek to last frame
       
   124         // because it's array index, so last index will be total length -1
       
   125         frameNumber=(frameNumber == totalNoOfFrames) ? (frameNumber-1) : frameNumber;
       
   126         try
   100         try
   127         {
   101         {
   128             long mediaTime1 = iPlayer.setMediaTime(mediaTime);
   102             iPlayer.setMediaTime(mediaTime);
   129         }
   103         }
   130         catch (MediaException e)
   104         catch (MediaException e)
   131         {
   105         {
   132             // Just ignore the exception
   106             // Just ignore the exception
   133             e.printStackTrace();
   107             e.printStackTrace();
   152      *
   126      *
   153      *  @return the actual number of frames skipped.
   127      *  @return the actual number of frames skipped.
   154      */
   128      */
   155     public int skip(int aFramesToSkip)
   129     public int skip(int aFramesToSkip)
   156     {
   130     {
   157         final String DEBUG_STR = "FramePositionControl::skip";
   131 //        final String DEBUG_STR = "FramePositionControl::skip";
   158         Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR+"+");
   132         //Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR+"+");
   159         // check the state of the player, if it closed throw exception
   133         // check the state of the player, if it closed throw exception
   160         checkState();
   134         checkState();
   161         int frameNumberToJump;
   135         //int frameNumberToJump;
   162         if (aFramesToSkip < 0)
   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)
   163         {
   140         {
   164             frameNumberToJump = 0;
   141             aFramesToSkip = iTotalNoOfFrames;
   165         }
   142         }
   166         // storing it in local variable, so that current frame index variable will be
       
   167         // consistent throughout this function, calling to getiFrameIndex(), each time
       
   168         // may return the different value.
       
   169         int currentFrameIndex=((AnimationPlayer) iPlayer).getiFrameIndex();
       
   170         // we are going to utilize the seek function here
   143         // we are going to utilize the seek function here
   171         // just get the current frame index from player and
   144         // just get the current frame index from player and
   172         // add it to the number of frame to skip
   145         // add it to the number of frame to skip
   173         frameNumberToJump = currentFrameIndex + aFramesToSkip;
   146         int seeked=seek(currentFrameIndex + aFramesToSkip);
   174         //if the frameNumberToJump > total no of frames, then skip to the last frame only
   147         int numberOfFrameSkipped=seeked-currentFrameIndex;
   175         frameNumberToJump =frameNumberToJump > iImagedata.length ? iImagedata.length-1 : frameNumberToJump;
   148         return numberOfFrameSkipped;
   176         seek(frameNumberToJump);
       
   177         Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR+"-");
       
   178         return frameNumberToJump - currentFrameIndex;
       
   179     }
   149     }
   180 
   150 
   181 }
   151 }