javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/animation/AnimationPlayer.java
changeset 61 bf7ee68962da
parent 48 e0d6e9bd3ca7
child 79 2f468c1958d0
--- a/javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/animation/AnimationPlayer.java	Tue Jul 06 14:10:26 2010 +0300
+++ b/javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/animation/AnimationPlayer.java	Wed Aug 18 09:43:15 2010 +0300
@@ -16,19 +16,14 @@
 */
 package com.nokia.microedition.media.animation;
 
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.Enumeration;
 import java.util.Hashtable;
-import java.util.Timer;
-import java.util.TimerTask;
 
 import javax.microedition.media.Control;
 import javax.microedition.media.MediaException;
-import javax.microedition.media.Player;
 import javax.microedition.media.PlayerListener;
 import javax.microedition.media.protocol.DataSource;
 
@@ -48,8 +43,6 @@
 import com.nokia.microedition.media.InputStreamSourceStream;
 import com.nokia.microedition.media.PlayerBase;
 import com.nokia.microedition.media.PlayerListenerImpl;
-import com.nokia.mj.impl.nokialcdui.LCDUIInvoker;
-import com.nokia.mj.impl.utils.Logger;
 
 public class AnimationPlayer extends PlayerBase implements ESWTinitializeListener
 {
@@ -98,7 +91,7 @@
     // of VideoControl should return the actual width and height of the image
     private Point iSourceDimension;
     // Current dimension of the image, MIDlet developer may change the size of VideoControl
-//    private Point iCurrentVideoDimension;
+    private Point iCurrentVideoDimension;
     // Total time taken so far to player the animation, it keeps updating as player moves
     private long iMediaTime;
     // Time at which, player should be stopped
@@ -121,8 +114,9 @@
     /**
      *
      * @param ds DataSource which contains the data to be displayed
+     * @throws MediaException
      */
-    public AnimationPlayer(DataSource ds)
+    public AnimationPlayer(DataSource ds) throws MediaException
     {
         iPlayerListenerImpl= new PlayerListenerImpl(this);
         //TODO check if we can do it in better way
@@ -135,7 +129,14 @@
         if (is!=null)
         {
             ImageLoader imageLoader= new ImageLoader();
+
+            // If it is any other format other than the image(jpeg, png and gif),
+            // following line will throw the SWT exception
             iImageData=imageLoader.load(is);
+            // If the image is loaded properly, we need to check whether it is GIF image or not.
+            // It can be PNG and JPEG as well
+            if (iImageData[0].type!=SWT.IMAGE_GIF)
+                throw new MediaException("Could not create player");
             try
             {
                 is.close();
@@ -145,6 +146,7 @@
                 e.printStackTrace();
             }
             iSourceDimension= new Point(imageLoader.logicalScreenWidth, imageLoader.logicalScreenHeight);
+            iCurrentVideoDimension= new Point(imageLoader.logicalScreenWidth, imageLoader.logicalScreenHeight);
             iBackgroundPixel= imageLoader.backgroundPixel;
         }
         populateControl();
@@ -156,13 +158,19 @@
      * @param locator
      * @throws SWTException
      */
-    public AnimationPlayer(String locator) throws SWTException
+    public AnimationPlayer(String locator) throws SWTException, MediaException
     {
-        iPlayerListenerImpl= new PlayerListenerImpl(this);
         ImageLoader imageLoader= new ImageLoader();
-        // Following line may throw SWTException
+        // If it is any other format other than the image(jpeg, png and gif),
+        // following line will throw the SWT exception
         iImageData=imageLoader.load(locator);
+        // If the image is loaded properly, we need to check whether it is GIF image or not.
+        // It can be PNG and JPEG as well
+        if (iImageData[0].type!=SWT.IMAGE_GIF)
+            throw new MediaException("Could not create player");
+        iPlayerListenerImpl= new PlayerListenerImpl(this);
         iSourceDimension= new Point(imageLoader.logicalScreenWidth, imageLoader.logicalScreenHeight);
+        iCurrentVideoDimension= new Point(imageLoader.logicalScreenWidth, imageLoader.logicalScreenHeight);
         iBackgroundPixel= imageLoader.backgroundPixel;
         populateControl();
     }
@@ -206,7 +214,8 @@
     {
         // since after stopping the player the player state will move to pre-fetched state
         changePlayerState(PREFETCHED);
-        iPlayerListenerImpl.postEvent(PlayerListener.STOPPED, new Long(iMediaTime * 10000));
+        //iPlayerListenerImpl.postEvent(PlayerListener.STOPPED, new Long(iMediaTime * 10000));
+        iPlayerListenerImpl.postEvent(PlayerListener.STOPPED, new Long(iMediaTime));
     }
 
     /**
@@ -251,8 +260,7 @@
     public long getMediaTime()
     {
         closeCheck();
-        // Since we have to return it in microsecond multiply it with 10000;
-        return iMediaTime*10000;
+        return iMediaTime;
     }
 
     public int getState()
@@ -272,12 +280,11 @@
     /**
      *
      */
-
+    GC gc = null;
     public void start() throws MediaException
     {
         final String DEBUG_STR = "AnimationPlayer::start()";
-        final long inTime = System.currentTimeMillis();
-
+//        final long inTime = System.currentTimeMillis();
         prefetch();
         // Only prefetched player may be started. If player is already started
         // this method returns silently.
@@ -288,21 +295,21 @@
             Thread thread = new Thread("Animation")
             {
                 int loopCount = iCurrentLoopCount;
-                GC gc = null;
                 public void run()
                 {
                     //changePlayerState(STARTED);
                     final int noOfFrames = iImageData.length;
                     while (iState == STARTED)
                     {
-                        final int delayTimeForNextFrame = iImageData[iFrameIndex].delayTime;
+                        final int delayTimeForNextFrame = iImageData[iFrameIndex].delayTime*10000 ;
                         // Since we are going to display first frame, notify all
                         // PlayerListener that Player has started
                         if (iFrameIndex == 0)
                         {
+                            iMediaTime=0;
                             iPlayerListenerImpl.postEvent(
-                                PlayerListener.STARTED, new Long(
-                                    iMediaTime * 10000));
+                                PlayerListener.STARTED, new Long(0));
+
                         }
                         if (iDisplay != null)
                         {
@@ -310,9 +317,15 @@
                             {
                                 public void run()
                                 {
-                                    if (gc == null)
-                                        gc = new GC(iImage);
-                                    Image tempImage = new Image(iDisplay,iImageData[iFrameIndex]);
+                                    // For out of memory issue in case of full screen, we are scaling the image
+                                    // while displaying it.
+                                    ImageData tempImageData =iImageData[iFrameIndex] ;
+                                    if (iSourceDimension.x!=iCurrentVideoDimension.x || iSourceDimension.x!=iCurrentVideoDimension.x)
+                                    {
+                                        tempImageData = iImageData[iFrameIndex].scaledTo(iCurrentVideoDimension.x , iCurrentVideoDimension.y);
+                                    }
+                                    Image tempImage = new Image(iDisplay,tempImageData);
+                                    //Image tempImage = new Image(iDisplay,iImageData[iFrameIndex]);
                                     gc.drawImage(tempImage, 0, 0);
                                     tempImage.dispose();
                                     iFrameIndex = (iFrameIndex + 1) % noOfFrames;
@@ -324,18 +337,17 @@
                                 }
                             });
                         }
-                        else// if the initDisplayMode is not called yer
+                        else// if the initDisplayMode is not called yet
                         {
                             iFrameIndex = (iFrameIndex + 1) % noOfFrames;
                             iMediaTime += delayTimeForNextFrame;
                         }
                         try
                         {
-                            Thread.sleep(delayTimeForNextFrame * 10);
+                            Thread.sleep(delayTimeForNextFrame / 1000);
                         }
                         catch (InterruptedException e)
                         {
-                            // TODO Auto-generated catch block
                             e.printStackTrace();
                         }
                         // post EOM event
@@ -352,22 +364,18 @@
                             // listener
                             iPlayerListenerImpl.postEvent(
                                 PlayerListener.END_OF_MEDIA,
-                                new Long(iMediaTime * 10000));
-                            iMediaTime = 0;
+                                new Long(iMediaTime));
                         }
                         if (iAnimationObserver!=null)
                         {
-                            iAnimationObserver.animationAdvanced(iMediaTime*10000);
+                            iAnimationObserver.animationAdvanced(iMediaTime);
                         }
                     }// end of while loop
                     iCurrentLoopCount = loopCount;
-                    // Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo, DEBUG_STR
-
                 }
             };
             thread.start();
         }
-        // Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,DEBUG_STR + "-");
     }
 
     /**
@@ -573,7 +581,15 @@
     public void notifyDisplayAvailable(Display aDisplay)
     {
         iDisplay= aDisplay;
-        iImage=new Image(iDisplay, iImageData[0]);
+        //iImage=new Image(iDisplay, iImageData[0]);
+        iImage=new Image(iDisplay, iImageData[iFrameIndex]);
+        iDisplay.syncExec(new Runnable()
+        {
+            public void run()
+            {
+                gc = new GC(iImage);
+            }
+        });
     }
     /**
      * Notified when the control is available
@@ -593,14 +609,17 @@
      * @param width : to be set of the video(Animated GIF)
      * @param height : height to be set of video(Animated GIF)
      */
-    void updateImageData(int width, int height)
+    void updateImageData(int aWidth, int aHeight)
     {
-        int noOfFrames= iImageData.length;
-        for (int i=0; i<noOfFrames; i++)
-        {
-            iImageData[i]=iImageData[i].scaledTo(width, height);
-        }
-        iImage=new Image(iDisplay, iImageData[iFrameIndex]);
+//        int noOfFrames= iImageData.length;
+//        for (int i=0; i<noOfFrames; i++)
+//        {
+//            iImageData[i]=iImageData[i].scaledTo(aWidth, aHeight);
+//        }
+//        iImage=new Image(iDisplay, iImageData[iFrameIndex]);
+        iCurrentVideoDimension.x=aWidth;
+        iCurrentVideoDimension.y= aHeight;
+        iImage=new Image(iDisplay, iImageData[iFrameIndex].scaledTo(aWidth, aHeight));
     }
 
     /**
@@ -641,10 +660,10 @@
      * Called from VideoControl to get the image width and height,
      * so that Item will be created exactly of same dimension
      */
-    Point getImageDimension()
+    Point getCurrentVideoDimension()
     {
-//      return iCurrentVideoDimension;
-        return new org.eclipse.swt.graphics.Point(iImageData[0].width, iImageData[0].height);
+        return iCurrentVideoDimension;
+        // return new org.eclipse.swt.graphics.Point(iImageData[0].width, iImageData[0].height);
     }
 
 
@@ -661,7 +680,6 @@
         iDisplayLocation.x=aX;
         iDisplayLocation.y=aY;
     }
-
     /**
      * @return the position of the image to be displayed
      */
@@ -749,7 +767,7 @@
         this.iIsControlVisible = iIsControlVisible;
     }
     /**
-     * Called from
+     * Called from StopTimeControl.animationAdvanced(long), once the stopped time is greater than or equal media time.
      */
     void postEvent(long aMediaTime)
     {