Android视频播放项目总结之 使用Android中的videoView自己定义,暂停,播放时间,总时间,进度等。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    >


    <VideoView
        android:id="@+id/activity_video_player_vd"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        />
    <include layout="@layout/contron_player"/>


</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control_player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_player_bottom_seekbar"
        android:gravity="center_vertical"
        android:orientation="horizontal" >


        <TextView
            android:id="@+id/current_tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="当前时间"
            android:textColor="#ffffff" />


        <SeekBar
            android:id="@+id/current_sk_seekbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_weight="1"
            android:maxHeight="5dip"
            android:minHeight="5dip"
            android:progress="20"
            android:progressDrawable="@drawable/progress_horizontal"
            android:thumb="@drawable/progress_thumb" />


        <TextView
            android:id="@+id/current_tv_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:text="总时间"
            android:textColor="#ffffff" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_player_bottom_control"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/current_btn_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/play_bg" />


        <Button
            android:id="@+id/current_btn_player"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/switch_player_bg" />
    </LinearLayout>


</LinearLayout>




public class VideoPlayerActivity extends Activity {


    private Boolean            isDestroy                = false;
    protected static final int PROGRESS                 = 0;
    private Uri                uri                      = null;
    private VideoView          activity_video_player_vd = null;
    private TextView           current_tv_time;
    private TextView           current_tv_duration;
    private SeekBar            current_sk_seekbar;
    private Button             current_btn_pause;
    private Button             current_btn_player;
    private Boolean            myPlaying                = true;
    private Utils              utils                    = null;


    Handler                    handler                  = new Handler() {
                                                            @SuppressLint("NewApi")
                                                            public void handleMessage(android.os.Message msg) {
                                                                switch (msg.what) {
                                                                    case PROGRESS:


                                                                        //的到当前进度
                                                                        int currentPosition = activity_video_player_vd
                                                                            .getCurrentPosition();


                                                                        current_tv_time
                                                                            .setText(utils
                                                                                .stringForTime(currentPosition));
                                                                        //得到总时长
                                                                        int duration = activity_video_player_vd
                                                                            .getDuration();
                                                                        current_tv_duration
                                                                            .setText(utils
                                                                                .stringForTime(duration));
                                                                        //更新seeKBar的进度
                                                                        current_sk_seekbar
                                                                            .setProgress(currentPosition);


                                                                        if (!isDestroyed()) {
                                                                            handler
                                                                                .sendEmptyMessageDelayed(
                                                                                    PROGRESS,
                                                                                    1 * 1000);


                                                                        }


                                                                        break;


                                                                }


                                                            };
                                                        };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        init();//初始化控件
        Listener();//VideoView监听事件
        current_btn_pause.setOnClickListener(myListoner);//播放暂停监听事件


    }


    @Override
    protected void onDestroy() {
        isDestroy = true;
        super.onDestroy();
    }


    /**
     * VideoView监听事件
     */
    private void Listener() {
        activity_video_player_vd.setVideoURI(uri); //设置播放的绝对地址
        /*
         * 开始播放监听
         */
        activity_video_player_vd.setOnPreparedListener(new OnPreparedListener() {


            @Override
            public void onPrepared(MediaPlayer mp) {
                activity_video_player_vd.start();
                myPlaying = activity_video_player_vd.isPlaying();
                if (myPlaying) {
                    current_btn_pause.setBackgroundResource(R.drawable.pause_bg);
                } else {
                    current_btn_pause.setBackgroundResource(R.drawable.play_bg);
                }
                /*发消息开始刷新进度**/
                handler.sendEmptyMessage(PROGRESS);
                current_sk_seekbar.setMax(activity_video_player_vd.getDuration());


            }
        });
        /*
         * 完成播放监听
         */
        activity_video_player_vd.setOnCompletionListener(new OnCompletionListener() {


            @Override
            public void onCompletion(MediaPlayer mp) {
                Toast.makeText(getApplicationContext(), "播放结束", 1).show();
                finish();
            }
        });
        /*
         * 播放出错
         */
        activity_video_player_vd.setOnErrorListener(new OnErrorListener() {


            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {


                Toast.makeText(getApplicationContext(), "播放出错", 1).show();
                return true;
            }
        });
        //SeekBar进度的监听
        current_sk_seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            //当手指离开的时候回调这个方法
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }


            //当手指开始触摸的时候回调这个方法
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }


            //状态发送变化的时候回调这个方法
            //progress 指定的视频位置
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {//用户滑动的才响应
                    activity_video_player_vd.seekTo(progress);
                }
            }
        });


    }


    /**
     * 按钮的监听事件
     */
    OnClickListener myListoner = new OnClickListener() {


                                   @Override
                                   public void onClick(View v) {
                                       switch (v.getId()) {
                                           case R.id.current_btn_player:


                                               break;
                                           case R.id.current_btn_pause:
                                               if (myPlaying) {
                                                   activity_video_player_vd.pause();


                                                   current_btn_pause
                                                       .setBackgroundResource(R.drawable.play_bg);


                                               } else {
                                                   activity_video_player_vd.start();
                                                   current_btn_pause
                                                       .setBackgroundResource(R.drawable.pause_bg);


                                               }
                                               myPlaying = !myPlaying;


                                               break;


                                       }
                                   }
                               };


    /**
     * 初始化控件的方法
     */
    private void init() {


        Intent intent = getIntent();
        uri = intent.getData();
        activity_video_player_vd = (VideoView) findViewById(R.id.activity_video_player_vd);
        current_tv_time = (TextView) findViewById(R.id.current_tv_time);
        current_tv_duration = (TextView) findViewById(R.id.current_tv_duration);
        current_sk_seekbar = (SeekBar) findViewById(R.id.current_sk_seekbar);
        current_btn_pause = (Button) findViewById(R.id.current_btn_pause);
        current_btn_player = (Button) findViewById(R.id.current_btn_player);
        utils = new Utils();
    }


}



你可能感兴趣的:(Android视频播放项目总结之 使用Android中的videoView自己定义,暂停,播放时间,总时间,进度等。)