借鉴了网上的代码,自己调式跑通,期间遇到一些问题,借此记录一下。
MainActivity.java
package com.example.samplevideoplayer;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SurfaceHolder.Callback {
private static final String TAG = "MainActivity";
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private MediaPlayer mPlayer = null;
private SurfaceView sfv_show;
private SurfaceHolder surfaceHolder;
private Button btn_start;
private Button btn_pause;
private Button btn_stop;
//添加权限
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();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
Log.d(TAG,"onCreate finished");
}
private void bindViews(){
sfv_show = findViewById(R.id.sfv_show);
btn_start = findViewById(R.id.btn_start);
btn_pause = findViewById(R.id.btn_pause);
btn_stop = findViewById(R.id.btn_stop);
btn_start.setOnClickListener(this);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
//初始化SurfaceHolder类,SurfaceView的控制器
surfaceHolder = sfv_show.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setFixedSize(320,220);
}
public void surfaceCreated(SurfaceHolder holder){
/* mPlayer = MediaPlayer.create(MainActivity.this, R.raw.example);
mPlayer.setDisplay(surfaceHolder);*/
mPlayer = new MediaPlayer();
verifyStoragePermissions(this);
// Log.d(TAG,"new MediaPlayer()");
try {
Log.d(TAG,"setDataSource prepare");
//String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Movies/example.mp4";
String filePath ="/storage/emulated/0/Movies/example.mp4";
//"/sdcard/Movies/example.mp4"
Log.d(TAG,"setPath:"+filePath);
mPlayer.setDataSource(filePath);
Log.d(TAG,"setDataSource ok");
mPlayer.setDisplay(surfaceHolder);
Log.d(TAG,"setDisplay(surfaceHolder)");
mPlayer.prepare();
// mPlayer.start();
// Log.d(TAG,"start()");
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG,"setDataSource exception");
}
}
public void onClick(View v){
switch (v.getId()){
case R.id.btn_start:
mPlayer.start();
Log.d(TAG,"onclick:start");
break;
case R.id.btn_pause:
mPlayer.pause();
break;
case R.id.btn_stop:
mPlayer.stop();
break;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}
public void surfaceDestroyed(SurfaceHolder holder){}
protected void onDestroy(){
super.onDestroy();
if(mPlayer.isPlaying()){
mPlayer.stop();
}
mPlayer.release();
}
}
AndroidManifest.xml 添加手机读写权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.samplevideoplayer">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?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">-->
<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"
android:padding="5dp"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/sfv_show"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
<Button
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="终止"/>
</LinearLayout>