第5天MediaPlayer和SurfaceView高级应用-播放视频和弹幕
代码
Activity
package com.example.myapplication;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.PixelCopy;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class Main3Activity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView surfaceId;
private SurfaceView DanMu;
private SeekBar seek;
private MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private SurfaceHolder holder2;
private EditText edit;
private Button send;
// private ArrayList<S> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main3);
surfaceId = findViewById(R.id.surface_id);
DanMu = findViewById(R.id.DanMu);
seek = findViewById(R.id.seek);
edit = findViewById(R.id.edit);
send = findViewById(R.id.send);
DanMu = findViewById(R.id.DanMu);
mediaPlayer=new MediaPlayer();
surfaceHolder=surfaceId.getHolder();
surfaceHolder.addCallback(this);
seek.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) {
mediaPlayer.pause();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.start();
}
});
holder2=DanMu.getHolder();
holder2.addCallback(this);
DanMu.setZOrderOnTop(true);
holder2.setFormat(PixelFormat.TRANSLUCENT);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyThread().start();
}
});
}
public void click1(View view) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource("/mnt/sdcard/aaa.mp4");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
seek.setMax(mediaPlayer.getDuration());
Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
seek.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
public void click2(View view) {
mediaPlayer.pause();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(holder==this.surfaceHolder){
mediaPlayer.setDisplay(this.surfaceHolder);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
int count=20;
int app= (int) (Math.random()*500+100);
String str = edit.getText().toString();
int index=DanMu.getScrollX();
Canvas canvas;
while (true) {
if (count == index) {
canvas=null;
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//画笔
Paint paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.RED);
paint.setTextSize(50);//设置文字的大小
canvas = holder2.lockCanvas();
if (canvas == null) {
break;
}
canvas.drawColor(PixelFormat.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawText(str, count += 20, app, paint);
holder2.unlockCanvasAndPost(canvas);
}
// try {
// this.clone();
// } catch (CloneNotSupportedException e) {
// e.printStackTrace();
// }
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mediaPlayer.release();
mediaPlayer=null;
}
}
布局
<?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=".Main3Activity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SurfaceView
android:id="@+id/surface_id"
android:layout_width="match_parent"
android:layout_height="300dp" />
<SeekBar
android:id="@+id/seek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:layout_weight="1"
android:onClick="click1"
android:text="点击播放视频" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:layout_weight="1"
android:onClick="click2"
android:text="点击停止播放" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="9"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击发送弹幕"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<SurfaceView
android:id="@+id/DanMu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
效果图