Android image timing issues

2019-09-05 06:18发布

问题:

I have an Android app that needs to display images and videos in a loop, it has no problem with playing the videos, but when it gets to the images the screen goes blank and nothing happens. the program is still running just not displaying.

I'm using SurfaceView.setBackground() to draw the image because swapping between an ImageView and the SurfaceView that the videos are played on has caused problems before.

I was having a similar issue when displaying the videos and I solved it by removing the loop I had waiting for the video to be prepared so ideally I would like to remove the loop I have waiting for a timer to elapse.

I have a function to display the new media when the old one is finished and it also prepares the next one for when the current one is finished. If the media is a video this is called from the onComplete function which works fine. But if it is an image I have a wait loop.

What I want to do is have something like the MediaPlayer.onComplete() function to be called when the image has been displayed for the desired amount of time. Does anything like this exist?

回答1:

I have since changed this to use handlers because of some other error checking I needed to do. However the other way does work

mHandler.postdelayed(run, delay);

Runnable run = new Runnable(){

      public void run()
       {
          //run function
       }
     };

I figured it out. I made a new thread that ran a timer and calls a function in the main activity that updates the screen. The call to the function that updates the screen is called using runOnUiThread() so that it has permission to change the screen.

public class ImageTimer implements Runnable{


    private long dur, cur,start;
    private boolean needed, run;
    private Activity mAct;

    ImageTimer(boolean running, long duration, Activity act)
    {
        run = running;
        dur = duration;
        needed = false;
        mAct = act;
    }

    @Override
    public void run() 
    {
        while(run)
        {
            if(needed)
            {
                start = System.currentTimeMillis();
                cur = System.currentTimeMillis() - start;
                while(cur < dur)
                {
                    cur = System.currentTimeMillis() - start;
                }   
                needed = false ;
                mAct.runOnUiThread(new Runnable() 
                {
                     public void run() 
                     {

                        MainActivity.ImageDone();
                     }

                });
            }
        }
    }