3

I'm trying to make a Video Player android application in which I have to display an ImageButton on the top of Video on a specific Time. This is my layout

<FrameLayout
                android:id="@+id/videoSurfaceContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <SurfaceView
                    android:id="@+id/videoSurface"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <LinearLayout
                    android:id="@+id/thumbnail"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|end"
                    android:orientation="vertical"
                    android:visibility="gone" >

                    <ImageButton
                        android:id="@+id/imageButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/image" />

                    <TextView
                        android:id="@+id/time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TEST TEST"
                        android:textColor="#FFFFFF" />
                </LinearLayout>
            </FrameLayout>

and i have an ArrayList of type ThumbnailInfo where

public class ThumbnailInfo {
    int time; //time in milliseconds
    String body;


    ThumbnailInfo(int time,String body){
        this.time = time;
        this.body = body;
    }
}

So now I have time stored in the arraylist and i want to display the thumbnail 10 seconds before the specific time.

For e.g. Suppose time stored in arraylist is 00:40 , 01:30 , 04:50 (just to make the question clear ,time is stored in milliseconds). So when i'm playing the Video , I have to set the Visibility of thumbnail layout as VISIBLE at 00:30 , 01:20 , 04:40 and will set the visibility GONE at 00:41, 01:31 , 04:51

So my question is How can I check stored Time in ArrayList continuously and execute the above mentioned operation.

Currently what i have is the Current Position of the Video using mediaPlayer.getCurrentPosition();

Now I have to compare the current position of the video continuously with the time stored in ArrayList . Is there any observer which can make the task easy or any other approach.

P.S. : User can pause the video multiple times.

Any help is welcome. Thanx in advance !!!

Abhinav Dua
  • 63
  • 3
  • 10
  • http://stackoverflow.com/questions/43131866/button-that-have-a-time-set-to-be-shown-and-to-be-hidden visit this thank you – koroku Mar 31 '17 at 03:45

3 Answers3

1

To simple you should convert time from minute to second start with 0. Such as:

  • VISIBLE at 00:30 , 01:20 , 04:40 => ArrayList< Visible > with values 30, 80, 280

  • INVISIBLE at 00:41, 01:31 , 04:51 => ArrayList< Invisible > with values 41, 61, 281

      int seconds = 0;
      CountDownTimer t = new CountDownTimer(Long.MAX_VALUE ,1000) { // interval 1s  
    
                  // This is called every interval. (Every 1 seconds in this example)
                        public void onTick(long millisUntilFinished) {
                            checkVisible(ArrayList visible);// make visible button
                            checkInvisible(ArrayList invisible); // make invisible button
                            seconds++:
                        }
                        public void onFinish() {
                            System.out.println("finished"); 
                            seconds = 0;         
                        }
        }.start();
    

Remember call

  • t.cancel() when pause video
  • t.finish() when finish play video
  • reset seconds variable;
Khang Doan
  • 436
  • 3
  • 9
  • Thank you for the reply . I just wanted to ask that as in the answer below by @Zhli ,it is mentioned that User may pause the video, so will it work in that case also ? – Abhinav Dua Dec 06 '15 at 08:56
0

Try a timer:
Example:

Timer wox=new Timer();
wox.scheduleAtFixedRate(new TimerTask() {
            public void run() {

                runOnUiThread(new Runnable() {
                    public void run() {
//your actions
         }
                });

            }
        }, 0, 1000);//timer time
salih kallai
  • 879
  • 2
  • 13
  • 34
0

Considered that the user may pause the video and restart it or maybe due to bad network, the video will not conform to the time you have assumed. So showing or hiding the imageButton should not depend on a timer.

You may consider using a runnable inside a handler, and it will execute every 1 second to check the progress of video displaying and show or hide imageButton accordingly.

something like the following code:

    Handler handler = new Handler();

    Runnable runnable = new Runnable() {
    @Override
    public void run() {
        int progress = checkVideoProgress();
        determineShowOrHideImageButtonByProgress(progress);
        if (videoIsEnd()){
            handler.removeCallbacks(runnable);
        }else {
            handler.postDelayed(runnable,1000);//check every 1 second in this example.
        }
      }
    };

In a word, the visibility of the imageButton should rely on the progress of video instead of the real time of os.

Zhli
  • 380
  • 1
  • 10