直接上代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/surfaceView"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.ObjectAnimator;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
img = findViewById(R.id.img);
surfaceView.setZOrderOnTop(true);
surfaceView.getHolder().setFormat(Color.TRANSPARENT);
initView();
}
//图片渐变消失
public void animationView() {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(img, "alpha", 1f, 0f);
objectAnimator.setDuration(2000);
objectAnimator.start();
}
private void initView() {
mediaPlayer = new MediaPlayer();
//获取SurfaceHolder 可以通过该接口来操作SurfaceView中的Surface
SurfaceHolder surfaceHolder = surfaceView.getHolder();
//设置Meiaplayer的准备监听
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//准备完成后播放
mediaPlayer.start();
}
});
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
//当SurfaceView中Surface创建时回掉
//该方法表示Surface已经创建完成,可以在该方法中进行绘图操作
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.reset();
try {
//设置视屏文件图像的显示参数
mediaPlayer.setDisplay(holder);
String videoUrl1 = "android.resource://" + getPackageName() + "/" + R.raw.handwrite_tutorial_animation;
Uri uri = Uri.parse(videoUrl1);
//file.getAbsolutePath()本地视频
//uri 网络视频
mediaPlayer.setDataSource(MainActivity.this,uri);
//获取第一帧图片
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(MainActivity.this,uri);
Bitmap bitmap = retriever.getFrameAtTime(1+1L,MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
img.setImageBitmap(bitmap);
animationView();
retriever.release();
// mediaPlayer.setOnPreparedListener(mediaPlayer -> {
// mediaPlayer.setOnInfoListener((mp, what, extra) -> {
// if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
// surfaceView.setBackgroundColor(Color.TRANSPARENT);
// return true;
// });
// });
//prepare();表示准备工作同步进行,(准备工作在UI线程中进行)
//当播放网络视频时,如果网络不要 会报ARN 所以不采用该方法
//mediaPlayer.prepare();
//异步准备 准备工作在子线程中进行 当播放网络视频时候一般采用此方法
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
//当SurfaceView的大小发生改变时候触发该方法
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
//Surface销毁时回掉
//当Surface销毁时候,同时把MediaPlayer也销毁
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mediaPlayer!=null) {
mediaPlayer.stop();
//释放资源
mediaPlayer.release();
}
}
});
//设置 surfaceView点击监听
surfaceView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
break;
}
//返回True代表事件已经处理了
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
initView();
}
}
网友评论