如何避免动态壁纸中的变量重新初始化

时间:2014-01-24 11:03:21

标签: android

我有以下动态壁纸:

public class GLWallpaperVideoDemo extends GLWallpaperService {
public static final String folder = "video";
public static final String TAG = "GLWVD";
public static String videoName="VIDEOWALL.avi";
//video variables
public int videoWidth,videoHeight;
public boolean videoWideScreen=false;
VideoRenderer renderer = null;

public GLWallpaperVideoDemo() { 
super(); 
Log.e(TAG,"constructor()"); 
}

@Override
public void onCreate() {
Log.e(TAG,"onCreate()");
    super.onCreate();
//transfer video to sdcard
Log.d(TAG,"transferring video asset to sdcard");
copyVideoToCard();
Log.d(TAG,"transferred");
//if videoName == blankVideo, then don't load anything
//TODO
NativeCalls.initVideo();
Log.d(TAG,"Opening video");
NativeCalls.loadVideo("file:/"+"sdcard/"
              +GLWallpaperVideoDemo.videoName);
//set video dimensions (now that we opened the video)
videoWidth = NativeCalls.getVideoWidth();
videoHeight = NativeCalls.getVideoHeight();
videoWideScreen = ( videoWidth > videoHeight ) ? true : false;
}


private VideoEngine mEngine=null;

@Override
public Engine onCreateEngine() {
Log.e(TAG,"onCreateEngine()");
    mEngine = new VideoEngine();
return mEngine;
}

class VideoEngine extends GLEngine {

VideoEngine() { 
    super();
    Log.e(TAG,"VideoEngine VideoEngine()");
    if(renderer==null)renderer = new VideoRenderer(GLWallpaperVideoDemo.this, 
                 this);
    setRenderer(renderer);
    //setRenderMode(RENDERMODE_WHEN_DIRTY);
    setRenderMode(RENDERMODE_CONTINUOUSLY);
}

VideoRenderer getRenderer() { return renderer; }

}
} 

这是渲染器:

public class VideoRenderer implements GLWallpaperService.Renderer {
static private String TAG="Renderer>>>>>>>>>>>>";
static boolean runOnce = false;
//MediaPlayer mediaPlayer = MediaPlayer.create(MyApp.getContext(), R.raw.gunfireusedforboardstage);

//screen variables
int screenWidth=50,screenHeight=50;
int drawWidth, drawHeight; //dimensions of fit-to-screen video
int paddingX, paddingY; //padding for fit-to-screen-video
//texture variables
int powWidth,powHeight;
//pointers
GLWallpaperVideoDemo mParent;
GLWallpaperVideoDemo.VideoEngine mParentEngine;
//lock
static public Object lock = new Object();
//fps
long fpsTime;
public int framecount;

public VideoRenderer() { 
super();
Log.e(TAG,"Constructor()");
}

public VideoRenderer(GLWallpaperVideoDemo p, 
           GLWallpaperVideoDemo.VideoEngine e) {
super();
mParent = p;
mParentEngine = e;
Log.e(TAG,"constructor()");
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.e(TAG, "onSurfaceCreated()");

}

void process(int width, int height) {
setScreenDimensions( width, height );
Log.d(TAG,"Killing texture");
NativeCalls.closeOpenGL();
setTextureDimensions( screenWidth, screenHeight );
setFitToScreenDimensions( mParent.videoWidth, 
              mParent.videoHeight );
if ( !runOnce ) {
    Log.e(TAG,"Preparing frame");
    NativeCalls.prepareStorageFrame();
}
NativeCalls.initOpenGL();
runOnce = true;
}

//This gets called whenever you preview the wallpaper or set the
//wallpaper
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.e(TAG,"onSurfaceChanged()");
 synchronized(lock) {
     process(width, height);
 }
}

public void onDrawFrame(GL10 gl) {
synchronized(lock) {
    //Log.d(TAG,"Drawing ....");
    NativeCalls.getFrame(); // from video
    NativeCalls.drawFrame(); // using openGL
    if(framecount>300)framecount=0;
    framecount++;
    //Log.e("framecount",Integer.toString(framecount));

    if(framecount==117 || framecount==124 ||framecount==137 ||framecount==145||framecount==159||framecount==167)

    {new Thread(new Runnable() {
        public void run() {
            MediaPlayer mp= MediaPlayer.create(MyApp.getContext(), R.raw.gunfireusedforboardstage);
            mp.start();
            mp.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    mp.release();

                };
            });
        }
    }).start();}

    if (MyDebug.showFPS) {
    final float fpsRate;
        fpsRate = 1000f/((float) (SystemClock.uptimeMillis() 
                      - fpsTime) );
        fpsTime = SystemClock.uptimeMillis();
        Log.d(TAG, 
          TAG+"drawFrame(): fps: "
          +String.valueOf(fpsRate)
          );
    }
}
}

现在您在渲染器中看到变量framecount了吗?

每次打开壁纸的设置都会重新初始化!

结果是渲染器继续工作,但framecount再次设置为0,

结果是帧不再与MediaPlayer同步。

1 个答案:

答案 0 :(得分:0)

解决:

我将变量设为静态: - )