Android学习之路------VideoView

一、VideoView

1.主要接口

设置播放的资源,参数为视频路径

videoView.setVideoPath("system/test.mp4")

开始播放

videoView.start()

暂停播放

videoView.pause()

停止播放

videoView.stop()

设置准备完成的监听函数

videoView.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    //准备完成之后要做的事情
                }

            });
videoView.setOnInfoListener(new OnInfoListener(){

        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            Log.i(TAG,"info : "+what);
            // TODO Auto-generated method stub
            if(what == mp.MEDIA_INFO_VIDEO_RENDERING_START){
                //马上开始播放 数据准备完成
            }
            return false;
        }
videoView.setOnCompletionListener(new OnCompletionListener(){

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    //播放完成后要做的事情
                }

            });

2.Demo:

import java.io.File;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

    public Button playButton;
    public VideoView videoView;
    public VideoView nextView;
    public boolean playStatus = false;
    private String TAG = "screentest";
    //public Receiver receiver;
    public static boolean isRunFlag = false;
    private static int videoIndex = 1;
    public final int FINISH_ACTIVITY = 0;
    public static Handler handler;
    private ImageView image;
    private static ActivityManager manager;
    private static boolean running = true;
    private File file;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //playButton = (Button)findViewById(R.id.play);
        videoView = (VideoView)findViewById(R.id.videoView);
        //initVideoPath(0);
        RelativeLayout.LayoutParams layoutParams = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        videoView.setLayoutParams(layoutParams);

        if(manager==null){
            manager = 
            (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        }
        file = new File("/system/test1.mp4");
        videoView.setVideoPath(file.getPath());
        videoView.start();
        isRunFlag  = true;
        //nextView.setVisibility(View.GONE);
        videoView.setOnInfoListener(new OnInfoListener(){

        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            Log.i(TAG,"info : "+what);
            // TODO Auto-generated method stub
            if(what == mp.MEDIA_INFO_VIDEO_RENDERING_START){
                //马上开始播放 数据准备完成
            }
            return false;
        }

        });
        videoView.setOnCompletionListener(new OnCompletionListener(){

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            Log.i(TAG, "videoView VIDEO PLAY OVER");

            initVideoPath(1);
            videoView.start();

        }
        });
    }
    public void initVideoPath(int index){
        File file = null;
        switch(index)
        {
        case 0:file = new File("/system/test1.mp4");
            break;
        case 1:file = new File("/system/test2.mp4");
            break;
        default:break;
        }
        videoView.setVideoPath(file.getPath());
        Log.i(TAG, "path:"+file.getPath());
    }
    @Override
    protected void onDestroy(){

        if(videoView!= null){
            videoView.suspend();
        }
        super.onDestroy();
    }
    @Override
    protected void onPause(){
        finish();
        super.onPause();
    }

}

VideoView在start()之后开始准备资源的加载,在加载资源完成后,会在onPrepareListener()或者onInfoLineter()中获取到相应状态,要实现视频德尔连续播放,只需要在onCompleteListener()中设置下一个视频资源的路径,再start()就行.

videoView.setLayoutParams(layoutParams);

这段代码主要是实现videoView的全屏播放

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值