1

I'm stuck with this problem for 3 days. I have the following layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.confero.android.videoview.VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <RelativeLayout
        android:id="@+id/hitting_stumps_layout"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/video" >

        <ImageView
            android:id="@+id/hitting_stumps_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="2dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_hitting_the_stumps" />

        <ImageView
            android:id="@+id/hitting_stumps_decision"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_alignRight="@+id/hitting_stumps_image"
            android:layout_centerInParent="true"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_tick" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/impact_line_layout"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hitting_stumps_layout" >

        <ImageView
            android:id="@+id/impact_line_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_impact_in_line" />

        <ImageView
            android:id="@+id/impact_line_decision"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_alignRight="@+id/impact_line_image"
            android:layout_centerInParent="true"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_tick" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/pitched_line_layout"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/impact_line_layout" >

        <ImageView
            android:id="@+id/pitched_line_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_pitching_in_line" />

        <ImageView
            android:id="@+id/pitched_line_decision"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_alignRight="@+id/pitched_line_image"
            android:layout_centerInParent="true"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_tick" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/shot_offered_layout"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pitched_line_layout" >

        <ImageView
            android:id="@+id/shot_offered_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_shot_offered" />

        <ImageView
            android:id="@+id/shot_offered_decision"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_alignRight="@+id/shot_offered_image"
            android:layout_centerInParent="true"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/lbw_tick" />
    </RelativeLayout>

</RelativeLayout>

and I'm trying to make the image view visible at certain time as the video playback goes on. I have used a CountDownTimer.

/**
 *
 */

package com.confero.android.videoview;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class LbwTutorialActivity extends Activity {

    private String TAG = "LbwTutorialActivity";
    private VideoView video;
    private CountDownTimer videoTimer;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            if (msg.arg1 == 1) {
                handleShotOffered(msg.arg2);
            } else if (msg.arg1 == 2) {
                handlePitchedInline(msg.arg2);
            } else if (msg.arg1 == 3) {
                handleImpactLine(msg.arg2);
            } else if (msg.arg1 == 4) {
                handleHittingStumps(msg.arg2);
            }
        }
    };

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.main);

        Uri uri = Uri.parse("android.resource://com.confero.android.videoview/"
                + R.raw.lbw2_lh);

        handleShotOffered(0);
        handleImpactLine(0);
        handlePitchedInline(0);
        handleHittingStumps(0);

        if (uri != null) {

            video = (VideoView) findViewById(R.id.video);
            video.setVideoURI(uri);
            video.setKeepScreenOn(true);
            video.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    video.stopPlayback();
                    videoTimer.cancel();
                }
            });

            video.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    video.requestFocus();

                    videoTimer = new CountDownTimer(30000, 125) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                            // TODO Auto-generated method stub
                            int stopTime = video.getCurrentPosition();
                            Message message;

                            if (stopTime >= 1500 && stopTime < 1620) {

                                message = new Message();
                                message.arg1 = 1;
                                message.arg2 = 1;
                                handler.sendMessage(message);
                            } else if (stopTime >= 4100 && stopTime < 4220) {
                                message = new Message();
                                Log.d(TAG, "Pause 1");
                                try {
                                    message.arg1 = 1;
                                    message.arg2 = 0;
                                    handler.sendMessage(message);
                                    video.pause();
                                    message.arg1 = 2;
                                    message.arg2 = 1;
                                    handler.sendMessage(message);
                                    this.wait(1500);                                    
                                    video.start();                                  
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            } else if (stopTime >= 4220 && stopTime < 4340) {
                                message = new Message();
                                Log.d(TAG, "Pause2");
                                try {
                                    message.arg1 = 2;
                                    message.arg2 = 0;
                                    handler.sendMessage(message);
                                    video.pause();
                                    message.arg1 = 3;
                                    message.arg2 = 1;
                                    handler.sendMessage(message);
                                    this.wait(1500);
                                    video.start();
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            } else if (stopTime >= 9400 && stopTime < 9520) {
                                message = new Message();
                                Log.d(TAG, "Pause 3");
                                try {
                                    message.arg1 = 3;
                                    message.arg2 = 0;
                                    handler.sendMessage(message);
                                    video.pause();
                                    message.arg1 = 4;
                                    message.arg2 = 1;
                                    handler.sendMessage(message);
                                    this.wait(1500);
                                    video.start();
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onFinish() {
                            // TODO Auto-generated method stub
                        }
                    };

                    videoTimer.start();
                    video.start();                  
                }
            });
        }
    }

    private void handleShotOffered(int status) {

        if (status == 1) {
            ((ImageView) findViewById(R.id.shot_offered_image))
                    .setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.shot_offered_decision))
                    .setVisibility(View.VISIBLE);
        } else {
            ((ImageView) findViewById(R.id.shot_offered_decision))
                    .setVisibility(View.INVISIBLE);
            ((ImageView) findViewById(R.id.shot_offered_image))
                    .setVisibility(View.INVISIBLE);
        }
    }

    private void handlePitchedInline(int status) {

        if (status == 1) {
            ((ImageView) findViewById(R.id.pitched_line_image))
                    .setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.pitched_line_decision))
                    .setVisibility(View.VISIBLE);
        } else {
            ((ImageView) findViewById(R.id.pitched_line_decision))
                    .setVisibility(View.INVISIBLE);
            ((ImageView) findViewById(R.id.pitched_line_image))
                    .setVisibility(View.INVISIBLE);
        }
    }

    private void handleImpactLine(int status) {

        if (status == 1) {
            ((ImageView) findViewById(R.id.impact_line_image))
                    .setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.impact_line_decision))
                    .setVisibility(View.VISIBLE);
        } else {
            ((ImageView) findViewById(R.id.impact_line_decision))
                    .setVisibility(View.INVISIBLE);
            ((ImageView) findViewById(R.id.impact_line_image))
                    .setVisibility(View.INVISIBLE);
        }
    }

    private void handleHittingStumps(int status) {

        if (status == 1) {
            ((ImageView) findViewById(R.id.hitting_stumps_image))
                    .setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.hitting_stumps_decision))
                    .setVisibility(View.VISIBLE);
        } else {
            ((ImageView) findViewById(R.id.hitting_stumps_decision))
                    .setVisibility(View.INVISIBLE);
            ((ImageView) findViewById(R.id.hitting_stumps_image))
                    .setVisibility(View.INVISIBLE);
        }
    }
}

My problem is that the image views are not getting visible. Is there a problem with layout or the Java code?

Please help. Thanks in advance.

4

0 回答 0