VideoView小试牛刀

来源:互联网 发布:封闭式小区 知乎 编辑:程序博客网 时间:2024/05/22 01:00

主要代码如下:

MainActivity:

public class MainActivity extends BaseActivity {    private String video_url = "http://dlqncdn.miaopai.com/stream/MVaux41A4lkuWloBbGUGaQ__.mp4";    private VideoView mVideoView;    private SeekBar mSeekBar;    private LinearLayout mOperation,mOperationLayout;    private String TAG = "TAG";    private RelativeLayout mVideoLayout;    private TextView mTime,mBuffer;    private int duration;   //视频总时常 毫秒    @Override    protected void setAllClick() {        //控制操作 显示 还是 隐藏        mVideoLayout.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.i(TAG,"mVideo Click");                if(mOperationLayout.getVisibility()==View.VISIBLE){                    mOperationLayout.setVisibility(View.GONE);                }else{                    mOperationLayout.setVisibility(View.VISIBLE);                }            }        });        //Start        mOperation.getChildAt(0).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(mVideoView.isPlaying()){                    return;                }                Log.i(TAG,"start");                mVideoView.start();            }        });        //Stop  暂停        mOperation.getChildAt(1).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.i(TAG,"pause");                mVideoView.pause();            }        });        //forward        mOperation.getChildAt(2).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int seekPostion =   mVideoView.getCurrentPosition()+1000*15;                if(seekPostion>mVideoView.getDuration()){                    seekPostion =   mVideoView.getDuration()-1000;                }                mVideoView.seekTo(seekPostion);                Log.i(TAG, "forward");            }        });        //restart        mOperation.getChildAt(3).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mVideoView.seekTo(0);                Log.i(TAG, "restart");            }        });        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {            @Override            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {            }            @Override            public void onStartTrackingTouch(SeekBar seekBar) {            }            @Override            public void onStopTrackingTouch(SeekBar seekBar) {                int progress = seekBar.getProgress();                mVideoView.seekTo(progress);            }        });    }    /**     *  创建一个Handler     */    private Handler handler = new Handler();    /**     * 创建一个Runnble     */    private Runnable runnable = new Runnable() {        @Override        public void run() {            //获取当前播放进度            int currentPosition = mVideoView.getCurrentPosition();            mSeekBar.setProgress(currentPosition);            //设置SeekBar 缓冲显示            int bufferPercentage = mVideoView.getBufferPercentage();            if(bufferPercentage==100){                mSeekBar.setSecondaryProgress(mVideoView.getDuration());            }else{                mSeekBar.setSecondaryProgress(mVideoView.getDuration()/100*bufferPercentage);            }            //填充数据            inflatData(currentPosition);            handler.postDelayed(runnable,500);        }    };    private void inflatData(int currentPosition) {        //显示时间用        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");        mTime.setText(sdf.format(new Date(currentPosition)) + " / " + sdf.format(new Date(duration)));        //加载百分比        int bufferPercentage = mVideoView.getBufferPercentage();        mBuffer.setText("加载"+bufferPercentage+"%");    }    @Override    protected void process() {        mVideoView.setVideoURI(Uri.parse(video_url));        //添加视频准备好的监听        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {            @Override            public void onPrepared(MediaPlayer mp) {                Log.i(TAG, "video is prepared");                duration = mVideoView.getDuration();                //显示时间                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");                mSeekBar.setMax(duration);                //填充数据                inflatData(mVideoView.getCurrentPosition());                //告诉handler执行                handler.post(runnable);            }        });        //当视频播放完成后的监听        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {            @Override            public void onCompletion(MediaPlayer mp) {                Log.i(TAG, "video is completion");            }        });        //播放发生异常时        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {            @Override            public boolean onError(MediaPlayer mp, int what, int extra) {                Log.i(TAG,"video is Error");                return false;            }        });    }    @Override    protected void initView() {        mTime = (TextView) findViewById(R.id.time);        mBuffer = (TextView) findViewById(R.id.buffer);        mVideoView = (VideoView) findViewById(R.id.video_view);        mSeekBar = (SeekBar) findViewById(R.id.pro_seek);        mOperation = (LinearLayout) findViewById(R.id.opertion);        mOperationLayout = (LinearLayout) findViewById(R.id.opertion_layout);        mVideoLayout = (RelativeLayout) findViewById(R.id.video_layout);    }    @Override    protected void loadView() {        setContentView(R.layout.activity_main);    }}xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#000000"    >    <TextView        android:id="@+id/time"        android:text="准备加载数据..."        android:layout_width="wrap_content"        android:textColor="#ffffffff"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/buffer"        android:layout_alignParentRight="true"        android:text="缓冲0%"        android:layout_width="wrap_content"        android:textColor="#ffffffff"        android:layout_height="wrap_content" />    <RelativeLayout        android:id="@+id/video_layout"        android:layout_width="match_parent"        android:layout_height="match_parent">        <VideoView            android:id="@+id/video_view"            android:layout_centerInParent="true"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </RelativeLayout>    <LinearLayout        android:id="@+id/opertion_layout"        android:layout_alignParentBottom="true"        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <SeekBar            android:id="@+id/pro_seek"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <LinearLayout            android:id="@+id/opertion"            android:orientation="horizontal"            android:layout_width="match_parent"            android:layout_height="wrap_content">            <Button                android:id="@+id/start"                android:background="#ffff85d7"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="Start"                />            <Button                android:id="@+id/stop"                android:background="#ffffa900"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="Stop"                />            <Button                android:id="@+id/forward"                android:background="#ff1591ff"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="Forward"                />            <Button                android:id="@+id/reStart"                android:background="#ff31fe22"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="Restart"                />        </LinearLayout>    </LinearLayout></RelativeLayout>

源码位置:http://download.csdn.net/detail/lzq520210/9421215

0 0