视频播放器
效果图
java代码
// An highlighted block
package bw.com.unit6;
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.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import bw.com.unit1.R;
public class Main6Activity extends AppCompatActivity implements View.OnClickListener,SurfaceHolder.Callback {
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
MediaPlayer mediaPlayer;
SeekBar seekBar;
TextView textView;
TextView textView1;
CheckBox checkBox;
RadioButton radioButton1;
RadioButton radioButton2;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 100:
int num = msg.arg1;
textView1.setText(getTime(num));
break;
case 200:
int num1 = msg.arg1;
textView1.setText("00:"+num1);
break;
case 300:
int num2 = msg.arg1;
textView1.setText("00:"+num2);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
initView();
}
private void initView() {
surfaceView = findViewById(R.id.sv8);
seekBar = findViewById(R.id.seekbar);
textView = findViewById(R.id.textview);
textView1 = findViewById(R.id.textview1);
checkBox = findViewById(R.id.cb);
radioButton1 = findViewById(R.id.rb1);
radioButton2 = findViewById(R.id.rb2);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser){
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
radioButton1.setOnClickListener(this);
radioButton2.setOnClickListener(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
initMediaPlayer();
mediaPlayer.setDisplay(holder);//设置让surfaceView来播放视频
mediaPlayer.prepareAsync();
}
private void initMediaPlayer() {mediaPlayer = new MediaPlayer();
//播放map4文件源
Uri uri = Uri.parse("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4");
try {
mediaPlayer.setDataSource(this,uri);
} catch (IOException e) {
e.printStackTrace();
}
// mediaPlayer.setDataSource();
//设置一个回调函数,在mediaPlayer准备之后,会回调该函数
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.setLooping(true);
mediaPlayer.start();
int duration = mediaPlayer.getDuration();//获取视频时间的长度
String sumtime = getTime(duration);
textView.setText(sumtime);
seekBar.setMax(duration);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (mediaPlayer.getCurrentPosition()==mediaPlayer.getDuration()){
seekBar.setProgress(0);
}else{
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
Message message = Message.obtain();
message.what =100;
message.arg1 = mediaPlayer.getCurrentPosition();
handler.sendMessage(message);
}
},0,1000);
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.stop();//停止播放
mediaPlayer.release();//释放资源
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.cb:
break;
case R.id.rb1:
Log.e("EEE","11111111111");
Message message = Message.obtain();
message.what =200;
int i = mediaPlayer.getCurrentPosition()/1000;
i-=5;
message.arg1 = i;
mediaPlayer.seekTo(i*1000);
handler.sendMessage(message);
seekBar.setProgress(i*1000);
break;
case R.id.rb2:
Log.e("EEE","22222222");
Message message1 = Message.obtain();
message1.what =300;
int j = mediaPlayer.getCurrentPosition()/1000;
j+=5;
mediaPlayer.seekTo(j*1000);
message1.arg1 = j;
handler.sendMessage(message1);
seekBar.setProgress(j*1000);
break;
}
}
public String getTime(int l){
long sum = l / 1000;
int f= (int) (sum/60);
int m = (int) (sum%60);
String f1 = f+"";
String f2 = m+"";
if (f<10){
f1 = 0+""+f;
}
if (m<10){
f2 = 0+""+m;
}
return f1+":"+f2;
}
}
布局文件
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<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="bw.com.unit6.Main6Activity">
<SurfaceView
android:id="@+id/sv8"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="0dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:layout_centerInParent="true"
android:drawableTop="@drawable/shape2"
/>
<RadioButton
android:id="@+id/rb1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:button="@null"
android:drawableTop="@mipmap/daotui"
android:layout_toLeftOf="@id/cb"
/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:button="@null"
android:drawableTop="@mipmap/kuaijin"
android:layout_toRightOf="@id/cb"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10.00"
android:layout_alignParentLeft="true"
/>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textview1"
/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10.00"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
正玄波
效果图
// An highlighted block
package bw.com.day5;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;
import bw.com.day1.R;
public class Main5Activity extends AppCompatActivity implements SurfaceHolder.Callback{
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Canvas canvas;
Paint paint = new Paint();
Path path = new Path();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
surfaceView = findViewById(R.id.sv);
surfaceHolder = surfaceView.getHolder();
paint.setColor(Color.RED);
paint.setAntiAlias(true);//去锯齿
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
surfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
new DrawSinLineThread().start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
int startX = 10;//x起始坐标
int startY = 520;//y起始坐标
int endX = 500;//x结束坐标
int endY = 520;//y结束坐标
int YstartX = 10;//y轴的起始x坐标
int YendX = 10;//y轴的结束x坐标
int YstartY = 30;
int YendY = 1000;
class DrawSinLineThread extends Thread{
@Override
public void run() {
super.run();
boolean isxyOK = false;//是否已经画完
paint.setColor(Color.RED);
// canvas.drawColor(Color.WHITE);
int x= YstartX;
int y= startY;
path.moveTo(x,y);
while (true){
canvas = surfaceHolder.lockCanvas();//获取画布
if (!isxyOK){//如果x和y轴没有画,先画x、y轴
//画x轴
path.moveTo(startX,startY);
path.lineTo(endX,endY);
canvas.drawPath(path,paint);
path.moveTo(YstartX,YstartY);
path.lineTo(YendX,YendY);
canvas.drawPath(path,paint);
// canvas.drawLine(startX,startY,endX,endY,paint);
// //画y轴
// canvas.drawLine(YstartX,YstartY,YendX,YendY,paint);
isxyOK = true;
}else{//开始画正玄波
int newY = y-(int)(100*Math.sin((x-5)* 2 *Math.PI/150));//计算y坐标
path.lineTo(x,newY);//将path连接新的坐标点
x++;
canvas.drawPath(path,paint);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (x >450){//正玄波的x坐标长度大于450,停止画
break;
}
}
//释放
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
布局文件
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="bw.com.day5.Main5Activity">
<SurfaceView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>