Question

I am using the following code to run a gif movie

if (movieStart == 0) {
  movieStart = (int) now;
}

if (mMovie != null) {
  int relTime = (int) (now - moviestart);

  if (relTime > mMovie.duration()) {
    relTime = mMovie.duration();
  }

  mMovie.setTime(relTime);
  mMovie.draw(canvas, 
      getWidth() / 2 - mMovie.width() / 2, 
      getHeight() / 2 - mMovie.height() / 2);

My movie runs fine. My question is this: How do I know when the movie has finished playing? (of course I have an action that I need to start when the movie ends.)

by the way, I am using android.graphics.Movie

Was it helpful?

Solution

Supposing your pasted code sits within onDraw(), the movie should have played fully once after relTime >= mMovie.duration() evaluates to true. Your code already checks for it, so you should just fire your action instead of relTime = mMovie.duration();:

if (movieStart == 0) {
    movieStart = (int) now;
}

if (mMovie != null) {
    int relTime = (int) (now - moviestart);

mMovie.setTime(relTime < mMovie.duration() ? relTime : mMovie.duration());
mMovie.draw(canvas, 
    getWidth() / 2 - mMovie.width() / 2, 
    getHeight() / 2 - mMovie.height() / 2);

if (relTime >= mMovie.duration()) {
    // move has played once
    fireAction();  // or set a member variable or whatever
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top