Android image timing issues

旧巷老猫 提交于 2019-12-25 07:34:46

问题


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();
                     }

                });
            }
        }
    }


来源:https://stackoverflow.com/questions/17022784/android-image-timing-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!