安卓高级8 SurfaceView案例三 结合mediaplay播放视频
我们知道mediaplay无法直接播放视频所以我们结合Surface
package qianfeng.com.mediaplayerdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView surfaceView;
private SeekBar seekBar;
private SurfaceHolder holder;
private MediaPlayer player;
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
seekBar = (SeekBar) findViewById(R.id.seekBar);
// 实例化SurfaceView
holder = surfaceView.getHolder();
holder.addCallback(this);
// 固定SurfaceView的屏幕大小//然而我发现没什么用
holder.setKeepScreenOn(true);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 实例化播放器
player = MediaPlayer.create(this, R.raw.fade);
// 设置seekBar 的属性
seekBar.setMax(player.getDuration());
seekBar.setProgress(0);
// 设置监听器
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// fromUser true 表示 是 用户在用手指拖拽 导致 seekBar 滑动了
// false 表示 seekBar在通过setProgress方法 改变
if (fromUser)
player.seekTo(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// 开始拖拽
player.pause();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 结束拖拽
player.start();
}
});
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bt:
//点击开始播放
if (player != null && !player.isPlaying()) {
player.start();
}
break;
case R.id.pause_bt:
if (player != null && player.isPlaying()) {
player.pause();
}
break;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (player != null) {
// 设置显示预览
player.setDisplay(holder);
}
// 开启线程 循环获取 播放进度 使用Hanlder 更新seekbar
new ProgressThread().start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
class ProgressThread extends Thread {
@Override
public void run() {
super.run();
while (flag) {
try {
// 每一秒 发送一次
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = Message.obtain();
// 获取 当前的 播放进度更新 seekBar
message.arg1 = player.getCurrentPosition();
handler.sendMessage(message);
}
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int arg1 = msg.arg1;
// 更新SeekBar
seekBar.setProgress(arg1);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
flag = false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="qianfeng.com.mediaplayerdemo.MainActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="400dp" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/start_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="播放" />
<Button
android:id="@+id/pause_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="暂停" />
</LinearLayout>
</LinearLayout>