布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<SurfaceView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="10"
android:id="@+id/surfaceView"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/play"
android:text="播放"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/pause"
android:text="暂停"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/stop"
android:text="停止"/>
</LinearLayout>
</LinearLayout>
main:
package com.example.media;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button play,pause,stop;
private boolean noPlay=true;//播放状态
MediaPlayer mediaPlayer;
SurfaceHolder surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
SurfaceView surfaceView=findViewById(R.id.surfaceView);
surfaceHolder=surfaceView.getHolder();
mediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(MainActivity.this, "视频播放完毕", Toast.LENGTH_SHORT).show();
}
});
play=findViewById(R.id.play);
pause=findViewById(R.id.pause);
stop=findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(noPlay) {
play();
noPlay = false;
}
else
{
mediaPlayer.start();//继续播放视频
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
noPlay=true;
}
}
});
}
public void play()//播放视频
{
mediaPlayer.reset();//重置media
mediaPlayer.setDisplay(surfaceHolder);//把视频画面输出到surfaceView
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/video.mp4");
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
@Override//方法资源
protected void onDestroy() {
super.onDestroy();
if(mediaPlayer!=null)
{
if(mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.release();
}
}
//设置读写权限
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
public static void verifyStoragePermissions(Activity activity) {
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}