一、效果
二、代码示例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom"/>
<RelativeLayout
android:id="@+id/bottom"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<CheckBox
android:id="@+id/playorpause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:background="@drawable/button_play"
android:layout_centerInParent="true"/>
<ImageView
android:id="@+id/privous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/privous"
android:layout_marginRight="30dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/playorpause"/>
<ImageView
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/next"
android:layout_toRightOf="@id/playorpause"
android:layout_marginLeft="30dp"/>
<LinearLayout
android:layout_below="@id/playorpause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/nowTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textSize="20sp"/>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"/>
<TextView
android:id="@+id/endTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="01:20"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
package day01.bw.com.day0612homework;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener {
private MediaPlayer player;
private CheckBox playorpause;
private ImageView privous;
private ImageView next;
private TextView nowTime;
private TextView endTime;
private SeekBar seekBar;
private boolean isPlaying;
private boolean isPause;
private SurfaceView surfaceView;
private SurfaceHolder holder;
private int time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void initMediaPlayer() {
player = new MediaPlayer();
try {
Uri uri = Uri.parse("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4");
player.setDataSource(this,uri);
} catch (IOException e) {
e.printStackTrace();
}
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.setLooping(true);
int duration = player.getDuration();
seekBar.setMax(duration);
String second = duration/1000%60+"";
if(second.length()==1){
second = "0"+second;
}
String minute = duration/1000/60+"";
if(minute.length()==1){
minute = "0"+minute;
}
endTime.setText(minute+":"+second);
player.start();
Message message = Message.obtain();
message.arg1=time;
handler.sendMessage(message);
isPlaying=true;
}
});
}
private void init() {
playorpause = findViewById(R.id.playorpause);
privous = findViewById(R.id.privous);
next = findViewById(R.id.next);
nowTime = findViewById(R.id.nowTime);
endTime = findViewById(R.id.endTime);
seekBar = findViewById(R.id.seekBar);
surfaceView = findViewById(R.id.surfaceView);
holder = surfaceView.getHolder();
holder.addCallback(this);
seekBar.setOnSeekBarChangeListener(this);
playorpause.setChecked(true);
playorpause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(isPause){
player.start();
openHandler();
}
}else{
if(isPlaying){
player.pause();
handler.removeCallbacksAndMessages(null);
isPause=true;
}
}
}
});
privous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
time-=10;
setNowTime(time);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
time+=10;
setNowTime(time);
}
});
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
seekBar.setProgress(time * 1000);
Message message = Message.obtain();
time++;
setNowTime(time);
handler.sendMessageDelayed(message,1000);
}
};
@Override
public void surfaceCreated(SurfaceHolder holder) {
initMediaPlayer();
player.setDisplay(holder);
player.prepareAsync();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
openHandler();
}
private void openHandler(){
player.seekTo(seekBar.getProgress());
handler.removeCallbacksAndMessages(null);
Message message = Message.obtain();
time = (seekBar.getProgress() / 1000);
setNowTime(time);
handler.sendMessage(message);
}
private void setNowTime(int time){
String second = time%60+"";
if(second.length()==1){
second = "0"+second;
}
String minute = time/60+"";
if(minute.length()==1){
minute = "0"+minute;
}
nowTime.setText(minute+":"+second);
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}