VideoView的使用

主布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="net.bwie.videoview.activity.MainActivity">

    <Button
        android:id="@+id/next_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一页"/>

    <Button
        android:id="@+id/play_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放"/>

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button mNextBtn;
    protected Button mPlayBtn;
    protected VideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.next_btn) {

            VideoListActivity.startActivity(this);

        } else if (view.getId() == R.id.play_btn) {
//            String rootPath = Environment.getExternalStorageDirectory().getPath();// 根目录
//            String fileName = "VID_20171117_144736.3gp";
//            String videoPath = rootPath + "/" + fileName;

            String videoPath = "http://172.29.23.6:8080/video/aa.3gp";
            playVideo(videoPath);
        }
    }

    private void playVideo(String videoPath) {
        mVideoView.setVideoPath(videoPath);
        // 设置多媒体控制器
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.start();
    }

    private void initView() {
        mNextBtn = (Button) findViewById(R.id.next_btn);
        mNextBtn.setOnClickListener(MainActivity.this);
        mPlayBtn = (Button) findViewById(R.id.play_btn);
        mPlayBtn.setOnClickListener(MainActivity.this);
        mVideoView = (VideoView) findViewById(R.id.video_view);
    }
}
bean类:

public class VideoBean {

    private List<String> pathList;

    public VideoBean() {
        pathList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            String path = Environment.getExternalStorageDirectory()
                    + "/VID_20171117_144736.3gp";
            pathList.add(path);
        }
    }

    public List<String> getPathList() {
        return pathList;
    }

    public void setPathList(List<String> pathList) {
        this.pathList = pathList;
    }
}
适配器:

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> {

    private Context mContext;
    private List<String> mPathList;

    // 当前正在播放的VideoView
    private VideoView mPlayingVideoView;
    //记录正在播放的进度TextView
    private TextView mPlayingProgressTextiew;

    private Handler mHandler;

    public VideoListAdapter(Context context, List<String> pathList) {
        mContext = context;
        mPathList = pathList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext)
                .inflate(R.layout.item_video, parent, false);
        return new ViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // 获取播放地址
        final String path = mPathList.get(position);

        // 如果VideoView滑出去时正在播放,下次复用进入的item应该停止播放并隐藏
        holder.mVideoView.stopPlayback();// 停止播放
        holder.mVideoView.setVisibility(View.INVISIBLE);// 设置不可见
        holder.mProgressTextView.setText("0%");


        // 播放按钮绑定监听器
        holder.mPlayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 停止上一个VideoView的播放并隐藏,才能继续播放当前的视频
                if (mPlayingVideoView != null) {
                    mPlayingVideoView.stopPlayback();
                    mPlayingVideoView.setVisibility(View.INVISIBLE);
                }

                // 播放时让VideoView可见
                holder.mVideoView.setVisibility(View.VISIBLE);// 设置可见性
                holder.mVideoView.setVideoPath(path);
                holder.mVideoView.start();

                // 记录当前正在播放的VideoView
                mPlayingVideoView = holder.mVideoView;
                mPlayingProgressTextiew = holder.mProgressTextView;

                // 提示开始播放的通知
                showPlayingNotification();

                // 点击播放按钮,使用Handler每间隔1s更新一次进度
                mHandler = new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message msg) {

                        // 获取VideoView当前进度和总进度
                        int currentPosition = mPlayingVideoView.getCurrentPosition();
                        int duration = mPlayingVideoView.getDuration();
                        float progress = currentPosition / ((float) duration) * 100;

                        // 展示进度百分比
                        mPlayingProgressTextiew.setText("0%");
                        holder.mProgressTextView.setText(progress + "%");
                        mHandler.sendEmptyMessageDelayed(1, 1000);
                        return true;
                    }
                });

                // 第一次让Handler更新进度
                mHandler.sendEmptyMessageDelayed(1, 1000);
            }
        });

    }

    // 展示开始播放的通知
    private void showPlayingNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
        // 必须设置3项:小图标,标题,内容
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("视频开始播放")
                .setContentText("正在播放");

        Notification notification = builder.build();

        // 使用通知管理者发布通知
        NotificationManager nm = (NotificationManager)
                mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        ;

        // 发布通知
        nm.notify(1, notification);
    }

    @Override
    public int getItemCount() {
        return mPathList == null ? 0 : mPathList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        Button mPlayBtn;
        VideoView mVideoView;
        TextView mProgressTextView;

        public ViewHolder(View itemView) {
            super(itemView);

            mPlayBtn = ((Button) itemView.findViewById(R.id.play_btn));
            mVideoView = ((VideoView) itemView.findViewById(R.id.video_view));
            mProgressTextView = ((TextView) itemView.findViewById(R.id.progress_tv));
        }
    }

}
item类
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <TextView
        android:id="@+id/progress_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前进度"/>

    <Button
        android:id="@+id/play_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放"/>

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:visibility="invisible"/>
    <!--android:visibility="invisible"代表视图不可见。点击播放时才可见-->

</LinearLayout>

public class VideoListActivity extends AppCompatActivity {

    protected RecyclerView mRecyclerView;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, VideoListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_video_list);
        initView();

        List<String> pathList = new VideoBean().getPathList();
        VideoListAdapter adapter = new VideoListAdapter(this, pathList);
        mRecyclerView.setAdapter(adapter);
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    }
}
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.bwie.videoview.activity.VideoListActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

</LinearLayout>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值