安卓视频播放页代码
package com.example.test1;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class ShipingBofActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shipingbof);
// 初始化
chushih();
}
// handler 更新进度条用
final int UpdataseekBar=1;
VideoView videoView;
SeekBar seekBar;
private void chushih() {
//返回按钮
ImageButton fanhui = findViewById(R.id.fanhui);
fanhui.setOnClickListener(v -> ShipingBofActivity.this.finish());
//得到传递的数据
Intent intent = getIntent();
videoView = findViewById(R.id.shipingbof);
videoView.setVideoURI(Uri.parse(intent.getStringExtra("shipy")));
/* //系统自带视频控制器
MediaController mediaContr=new MediaController(this);
//设置关联
mediaContr.setMediaPlayer(videoView);
videoView.setMediaController(mediaContr);*/
TextView shipingmz = findViewById(R.id.wzspm);
String spylj=intent.getStringExtra("shipm");
shipingmz.setText(spylj);
//进入该活动自动播放
videoView.start();
seekBar = findViewById(R.id.sbspbfjdt);
//请求焦点
videoView.requestFocus();
//播放和暂停按钮
Button bofan = findViewById(R.id.bnspbf), zantin = findViewById(R.id.bnspzt);
bofan.setOnClickListener(v -> {
videoView.start();
handler.sendEmptyMessage(UpdataseekBar);
});
zantin.setOnClickListener(v -> {
videoView.pause();
handler.removeMessages(UpdataseekBar);
});
//视频播放完监听事件,播完退出
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
handler.removeMessages(UpdataseekBar);
ShipingBofActivity.this.finish();
}
});
//开始移动进度条
handler.sendEmptyMessage(UpdataseekBar);
//进度条更改事件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//如果没有播放就将播放位置移动到指定位置。
if (!videoView.isPlaying()){
videoView.seekTo(seekBar.getProgress());
}
}
// 进度条开始触摸拖动
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// 移除监听以及暂停播放
handler.removeMessages(UpdataseekBar);
videoView.pause();
}
//进度条停止触摸
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//停止拖动开始播放视频与监听视频播放进度
videoView.start();
handler.sendEmptyMessage(UpdataseekBar);
}
});
}
Handler handler=new Handler(new Handler.Callback() {
@Override
synchronized public boolean handleMessage(@NonNull Message msg) {
switch (msg.what){
case UpdataseekBar:
{
//设置进度条的最大值
// 视频要等到加载完成播放才可以获取到总时长,因为Videoview的特性
seekBar.setMax(videoView.getDuration());
// 设置当前进度条的值
seekBar.setProgress(videoView.getCurrentPosition());
}
//每隔500毫秒刷新一次进度条的值;
handler.sendEmptyMessageDelayed(UpdataseekBar,500);
}
return true;
}
});
/**
* 打开该活动的静态方法
* @param context 上一个活动的context
* @param shipdiz 视频资源地址
* @param shipmz 视频名字
*/
public static void startActivity(Context context,String shipdiz,String shipmz){
Intent intent=new Intent(context,ShipingBofActivity.class);
intent.putExtra("shipy",shipdiz);
intent.putExtra("shipm",shipmz);
context.startActivity(intent);
}
}
本地视频文件位置
视频播放器活动页xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:background="@color/qianlan">
<ImageButton
android:id="@+id/fanhui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:background="#00FFFFFF"
android:src="@drawable/ic_baseline_assignment_return_24" />
<TextView
android:id="@+id/wzspm"
android:textSize="22sp"
android:textColor="@color/white"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="视频名"/>
</RelativeLayout>
<VideoView
android:id="@+id/shipingbof"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp" />
<SeekBar
style="@style/Widget.AppCompat.SeekBar"
android:layout_width="match_parent"
android:layout_height="16dp"
android:progressDrawable="@drawable/jdtsz"
android:thumb="@drawable/start_time"
android:thumbOffset="0dp"
android:id="@+id/sbspbfjdt"/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_marginRight="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:id="@+id/bnspbf"
android:backgroundTint="@drawable/dcehcdbj"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:id="@+id/bnspzt"
android:backgroundTint="@drawable/dcehcdbj"/>
</LinearLayout>
</LinearLayout>
运行视频