美文网首页
自定义本地视频播放器

自定义本地视频播放器

作者: 唔笛plk | 来源:发表于2018-11-07 14:42 被阅读0次

一、序言

公司的智能设备要添加个视频播放功能,不要求在线播放,而且视频也比较小,所以就不采用在线播放的一些库,简单的写个本地播放器~~功能比较简单直接上代码,

二、播放原理

使用的是MediaPlayer +SurfaceView,由于本地VideoView也是使用,但是对于视频格式有比较大的限制,所以采用MediaPlayer,稍微比较麻烦一些。

三、播放代码

  • 页面布局
<?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"
    android:background="@color/black">

    <SurfaceView
        android:id="@+id/svVideo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/tvBack"
        android:layout_width="match_parent"
        android:layout_height="120px"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/shape_video_top"
        android:drawableLeft="@drawable/video_back"
        android:gravity="center"
        android:paddingLeft="30px" />

    <RelativeLayout
        android:id="@+id/rlVideoBottom"
        android:layout_width="match_parent"
        android:layout_height="120px"
        android:layout_alignParentBottom="true"
        android:background="@drawable/shape_video_bottom"
        android:visibility="visible">

        <ImageView
            android:id="@+id/ivVideoControl"
            android:layout_width="56px"
            android:layout_height="56px"
            android:layout_centerVertical="true"
            android:layout_marginLeft="60px"
            android:src="@drawable/video_play_s" />

        <TextView
            android:id="@+id/tvVideoStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="54px"
            android:layout_toRightOf="@+id/ivVideoControl"
            android:text="00:00:00"
            android:textColor="@color/white"
            android:textSize="28px" />

        <SeekBar
            android:id="@+id/sbVideo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="30px"
            android:layout_marginRight="30px"
            android:layout_toLeftOf="@+id/tvVideoEnd"
            android:layout_toRightOf="@id/tvVideoStart" />

        <TextView
            android:id="@+id/tvVideoEnd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="54px"
            android:layout_marginRight="30px"
            android:text="00:00:00"
            android:textColor="@color/white"
            android:textSize="28px" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/ivVideoControlBig"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/video_play"
        android:visibility="visible" />
</RelativeLayout>
  • 播放代码
package com.gzjiequan.rescuewithyou.rescuesys.sosdevice;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.gzjiequan.rescuewithyou.R;
import com.gzjiequan.rescuewithyou.base.BaseActivity;
import com.gzjiequan.rescuewithyou.util.DateUtil;
import com.gzjiequan.rescuewithyou.util.FileAccessor;
import com.gzjiequan.rescuewithyou.util.LogUtil;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.io.File;
import java.io.IOException;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VideoPlayerActivity extends BaseActivity {


    @BindView(R.id.svVideo)
    SurfaceView svVideo;
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.ivVideoControl)
    ImageView ivVideoControl;
    @BindView(R.id.tvVideoStart)
    TextView tvVideoStart;
    @BindView(R.id.sbVideo)
    SeekBar sbVideo;
    @BindView(R.id.tvVideoEnd)
    TextView tvVideoEnd;
    @BindView(R.id.rlVideoBottom)
    RelativeLayout rlVideoBottom;
    @BindView(R.id.ivVideoControlBig)
    ImageView ivVideoControlBig;
    private String videoUrl = "";
    private int allTime, currentTime;
    private CountDownTimer countDownTimer;
    private MediaPlayer mediaPlayer;
    private File videoFile;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_video_player;
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        videoUrl = getIntent().getStringExtra(this.getClass().getSimpleName());
        if (!TextUtils.isEmpty(videoUrl)) {
            onStartProgress();
            videoFile = new File(FileAccessor.FILE + getIntent().getStringExtra("NAME") + FileAccessor.MP4);
            mediaPlayer=new MediaPlayer();
            if(!videoFile.exists()) {
                 // 下载视频,可以用其他方式下载
                FinalHttp finalHttp = new FinalHttp();
                finalHttp.download(videoUrl, videoFile.getAbsolutePath(), new AjaxCallBack<File>() {

                    @Override
                    public void onSuccess(File file) {
                        super.onSuccess(file);
                        // 开始加载视频
                        svVideo.getHolder().addCallback(new SurfaceHolder.Callback() {
                            @Override
                            public void surfaceCreated(SurfaceHolder holder) {
                                startVideo();
                            }

                            @Override
                            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                            }

                            @Override
                            public void surfaceDestroyed(SurfaceHolder holder) {

                            }
                        });
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        super.onFailure(t, errorNo, strMsg);
                    }
                });
            }else {
                svVideo.getHolder().addCallback(new SurfaceHolder.Callback() {
                    @Override
                    public void surfaceCreated(SurfaceHolder holder) {
                        startVideo();
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                    }

                    @Override
                    public void surfaceDestroyed(SurfaceHolder holder) {

                    }
                });
            }
        }
    }

    public void startVideo() {
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(videoFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.setDisplay(svVideo.getHolder());
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                allTime = mp.getDuration() - TimeZone.getDefault().getRawOffset();
                tvVideoEnd.setText(DateUtil.getVideoTime(allTime));
                sbVideo.setMax(mediaPlayer.getDuration() + 10);
                mediaPlayer.seekTo(sbVideo.getProgress() + 10);
                LogUtil.d(TAG, " onPrepared" + mp.getDuration());
                onStopProgress();
            }
        });

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                ivVideoControlBig.setVisibility(View.VISIBLE);
                ivVideoControl.setImageResource(R.drawable.video_play_s);
                rlVideoBottom.setVisibility(View.VISIBLE);
                tvBack.setVisibility(View.VISIBLE);
                if (countDownTimer != null) {
                    countDownTimer.cancel();
                }
            }
        });
        mediaPlayer.prepareAsync();
        countDownTimer = new CountDownTimer(24 * 60 * 60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                currentTime = mediaPlayer.getCurrentPosition() - TimeZone.getDefault().getRawOffset();
                tvVideoStart.setText(DateUtil.getVideoTime(currentTime));
                sbVideo.setProgress(mediaPlayer.getCurrentPosition());
                LogUtil.d(TAG, "getCurrentPosition" + mediaPlayer.getCurrentPosition());
            }

            @Override
            public void onFinish() {

            }
        };
        sbVideo.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) {
                LogUtil.d(TAG, "onStopTrackingTouch  " + seekBar.getProgress());
                mediaPlayer.seekTo(seekBar.getProgress());
                tvVideoStart.setText(DateUtil.getVideoTime(mediaPlayer.getCurrentPosition() - TimeZone.getDefault().getRawOffset()));
            }
        });

    }


    @Override
    protected void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer=null;
        }
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }

    @OnClick({R.id.tvBack, R.id.ivVideoControl, R.id.svVideo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.svVideo:
                if (mediaPlayer == null) {
                    return;
                }
                if (mediaPlayer.isPlaying()) {
                    ivVideoControlBig.setVisibility(View.VISIBLE);
                    ivVideoControl.setImageResource(R.drawable.video_play_s);
                    rlVideoBottom.setVisibility(View.VISIBLE);
                    tvBack.setVisibility(View.VISIBLE);
                    mediaPlayer.pause();
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                    }

                } else {
                    ivVideoControlBig.setVisibility(View.GONE);
                    ivVideoControl.setImageResource(R.drawable.video_stop_s);
                    rlVideoBottom.setVisibility(View.GONE);
                    tvBack.setVisibility(View.GONE);
                    mediaPlayer.start();
                    if (countDownTimer != null) {
                        countDownTimer.start();
                    }
                }
                break;
            case R.id.ivVideoControl:
                if (mediaPlayer == null) {
                    return;
                }
                if (mediaPlayer.isPlaying()) {
                    ivVideoControlBig.setVisibility(View.VISIBLE);
                    ivVideoControl.setImageResource(R.drawable.video_play_s);
                    rlVideoBottom.setVisibility(View.VISIBLE);
                    tvBack.setVisibility(View.VISIBLE);
                    mediaPlayer.pause();
                    if (countDownTimer != null) {
                        countDownTimer.cancel();
                    }
                } else {
                    ivVideoControlBig.setVisibility(View.GONE);
                    ivVideoControl.setImageResource(R.drawable.video_stop_s);
                    rlVideoBottom.setVisibility(View.GONE);
                    tvBack.setVisibility(View.GONE);
                    mediaPlayer.start();
                    if (countDownTimer != null) {
                        countDownTimer.start();
                    }
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}

相关文章

  • iOS-AVPlayer封装相关

    本篇涵盖AVPlayer相关知识点、封装自定义视频播放器等. 1.AVAudioPlayer本地音乐播放、后台播放...

  • 自定义本地视频播放器

    一、序言 公司的智能设备要添加个视频播放功能,不要求在线播放,而且视频也比较小,所以就不采用在线播放的一些库,简单...

  • Android遍历本地视频文件并且过滤

    最近在想写一个视频播放器玩玩,既然是播放器,肯定有播放本地视频的功能,那么问题来了,我们怎么获取本地一些视频呢?当...

  • iOS使用AVPlayer自定义视频播放器

    上一篇:iOS使用AVPlayer自定义音频播放器iOS内部提供的有三种视频播放的方式,且都能播放本地、远程的音频...

  • 小龙播放器

    小龙播放器是一款本地视频播放器,利用著名的视频解码框架:MobileVLCKit 进行视频解码,支持市面上主流的视...

  • 自定义播放器(FMGAVPlayer)

    https://github.com/MGXu/FMGAVPlayer FMGAVPlayer 自定义视频播放器,...

  • XLVideoPlayer——基于AVFoundation自定义

    XLVideoPlayer——基于AVFoundation自定义的视频播放器 XLVideoPlayer——基于A...

  • video.js相关文章

    1、免费视频播放器videojs中文教程2、HTML5视频播放器Video.Js的使用3、video.js 自定义...

  • Android 视频裁剪自定义 View

    PlaySeekbar - Github Android 视频裁剪自定义 View - 裁减播放的视频(本地视频)...

  • 手机影音03

    阅读原文 19-监听播放外界网络和本地视频&如何调起其他播放器 19.1监听播放外界网络和本地视频,参照Galle...

网友评论

      本文标题:自定义本地视频播放器

      本文链接:https://www.haomeiwen.com/subject/tykvxqtx.html