Как создать только один экземпляр медиапроигрывателя при воспроизведении нескольких видео, используя вид текстуры в обзоре реселлера.
В моем обзоре переработчика у меня есть этот проигрыватель видео ниже, когда я нажимаю на кнопку воспроизведения, он воспроизводит предыдущее видео из обзора переработчика, и два видео воспроизводятся одновременно, не могу понять, что я делаю неправильно в приведенном ниже коде, который создает два экземпляра медиаплеера., Как заставить медиаплеер воспроизводить по одному видео за раз?
public class VideoPlayer extends TextureView implements TextureView.SurfaceTextureListener, MediaPlayer.OnPreparedListener {
private String url;
private MediaPlayer mediaPlayer;
private OnVideoPreparedListener onVideoPreparedListener;
public VideoPlayer(Context context) {
super(context);
}
public VideoPlayer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void loadVideo(String localPath,OnVideoPreparedListener onVideoPreparedListener) {
this.url = localPath;
this.onVideoPreparedListener = onVideoPreparedListener;
if (this.isAvailable()) {
try {
prepareVideo(getSurfaceTexture());
} catch (Exception e) {
e.printStackTrace();
}
}
setSurfaceTextureListener(this);
}
public void videoPlayer(String path , boolean status){
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
try {
prepareVideo(surfaceTexture);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
if (mediaPlayer != null) {
surfaceTexture.release();
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
public void prepareVideo(SurfaceTexture t) throws Exception {
Surface surface = new Surface(t);
mediaPlayer = new MediaPlayer();
mediaPlayer.setSurface(surface);
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(this);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
onVideoPreparedListener.onVideoPrepared(mediaPlayer);
adjustAspectRatio(mediaPlayer.getVideoWidth(), mediaPlayer.getVideoHeight());
}
@Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
int j = View.MeasureSpec.getSize(paramInt2);
setMeasuredDimension(i, j);
}
private void adjustAspectRatio(int videoWidth, int videoHeight) {
int viewWidth = getWidth();
int viewHeight = getHeight();
double aspectRatio = (double) videoHeight / videoWidth;
int newWidth, newHeight;
if (viewHeight > (int) (viewWidth * aspectRatio)) {
newWidth = viewWidth;
newHeight = (int) (viewWidth * aspectRatio);
} else {
newWidth = (int) (viewHeight / aspectRatio);
newHeight = viewHeight;
}
int xoff = (viewWidth - newWidth) / 2;
int yoff = (viewHeight - newHeight) / 2;
Matrix txform = new Matrix();
getTransform(txform);
txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
txform.postTranslate(xoff, yoff);
setTransform(txform);
}}