这是在一项活动中显示多个视频的完整答案。在所有设备中工作。我使用 Surfaceview 和 Textureview 播放视频。
活动:
public class ReactionViewActivity extends AppCompatActivity {
private static final String TAG = ReactionViewActivity.class.getSimpleName();
private String mainVideo = "";
private ProgressBar progress_bar;
private SurfaceView mSurfaceView;
private TextureView mTextureView;
private MediaPlayer mMediaPlayer, mMediaPlayer1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reaction_view);
initView();
}
private void initView() {
progress_bar = findViewById(R.id.progress_bar);
mSurfaceView = findViewById(R.id.surface_view);
mTextureView = findViewById(R.id.textureView);
textureParams = (FrameLayout.LayoutParams) mTextureView.getLayoutParams();
surfaceParams = (FrameLayout.LayoutParams) mSurfaceView.getLayoutParams();
final String video = getIntent().getStringExtra("video");
mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer1 = new MediaPlayer();
mMediaPlayer1.setDataSource(video);
mMediaPlayer1.setSurface(surface);
mMediaPlayer1.setLooping(true);
mMediaPlayer1.prepareAsync();
// Play video when the media source is ready for playback.
mMediaPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
progress_bar.setVisibility(View.GONE);
mediaPlayer.start();
}
});
mMediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
});
if (Objects.requireNonNull(getIntent().getExtras()).containsKey("mainVideo")) {
mainVideo = getIntent().getStringExtra("mainVideo");
mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(surfaceHolder);
try {
mMediaPlayer.setDataSource(mainVideo);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mMediaPlayer.start();
}
});
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
});
}
}
@Override
public void onBackPressed() {
releaseMediaPlayer();
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
if (mMediaPlayer1 != null) {
mMediaPlayer1.release();
mMediaPlayer1 = null;
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ReactionViewActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:layout_below="@+id/rlTopbar">
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="110dp"
android:layout_height="140dp"
android:layout_gravity="right"
android:layout_marginRight="@dimen/margin_20"
android:layout_marginTop="@dimen/margin_20" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="@dimen/margin_50"
android:layout_height="@dimen/margin_50"
android:layout_gravity="center"
android:indeterminateDrawable="@drawable/custom_progress" />
</FrameLayout>
</RelativeLayout>