Android mediaplayer doesn’t work for the first time
I added mediaplayer to my application. It works normally, but when I first call the surface and initialize mediaplayer, the texture becomes visible, but the texture is white and there is no video (but the sound is normal. My log says it works normally)
After the first attempt, it works perfectly. But I can't find the reason why it doesn't work after the first attempt. This is my code:
/////////
mVideoView = (TextureView) mRootView.findViewById(R.id.show_video_content);
mVideoView.setSurfaceTextureListener(this);
////////
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Log.i(TAG, "InicializoSurface. Width: " + width + " HEIGHT:" + height);
Log.i(TAG, "InicializoSurface. Width: " + mVideoView.getMeasuredWidth() + " HEIGHT:" + mVideoView.getMeasuredHeight());
Log.i(TAG, "View transform. Width: " + mVideoView.getWidth() + " HEIGHT:" + mVideoView.getHeight());
mMediaSurface = new Surface(mVideoView.getSurfaceTexture());
initializeMediaPlayer();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (mMediaPlayer != null) {
// Make sure we stop video and release resources when activity is destroyed.
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
This is my custom mediaplayer and the event called when I initialize the surface:
private class CustomMediaPlayer extends MediaPlayer implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {
SurfaceHolder mVideoHolder;
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (mediaPlayer != null) {
mediaPlayer.start();
}
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG,"video preparado");
mMediaPrepared = true;
}
}
private void initializeMediaPlayer(){
mMediaPlayer = new CustomMediaPlayer();
Uri uri = Uri.parse(mCameraDataAdapter.getList().get(0).getPath());
try {
mMediaPlayer.setDataSource(mActivity, uri);
mMediaPlayer.setSurface(mMediaSurface);
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(mMediaPlayer);
mMediaPlayer.setOnCompletionListener(mMediaPlayer);
} catch (IOException e) {
e.printStackTrace();
}
}
Finally, I call start () in onDragListener:
case DragEvent.ACTION_DRAG_STARTED:
if (mCameraDataAdapter.getFirstElement().equalsIgnoreCase("video")) {
mThumbnailContainer.setVisibility(View.VISIBLE);
mVideoView.setVisibility(View.VISIBLE);
Log.i(TAG, "X: " + mVideoView.getX() + "Y: " + mVideoView.getY());
if (mVideoView.isAvailable()) {
onSurfaceTextureAvailable(mVideoView.getSurfaceTexture(), mVideoView.getWidth(), mVideoView.getHeight());
}
if (mMediaPlayer == null) {
initializeMediaPlayer();
}
if (mMediaPrepared) {
Log.i(TAG,"Comienzo Video");
mMediaPlayer.start();
}
} else {
mThumbnailContainer.setVisibility(View.VISIBLE);
mImageContainer.setVisibility(View.VISIBLE);
}
mPreviewThumbIcon.setVisibility(View.INVISIBLE);
return true;
I've tried to change my prepare() to prepareasync() or always use the "visible" view, but I can't find the reason why it doesn't work
Thank you very much in advance!
resolvent:
Invalid Sign
case DragEvent.ACTION_DRAG_STARTED:
selected = null;
if (mCameraDataAdapter.getFirstElement().equalsIgnoreCase("video")) {
mImageContainer.setVisibility(View.INVISIBLE);
mVideoView.setVisibility(View.VISIBLE);
//SurfaceTexture is not called if the Texture is already available, so we call it anyway
if (mVideoView.isAvailable()) {
onSurfaceTextureAvailable(mVideoView.getSurfaceTexture(), mVideoView.getMeasuredWidth(), mVideoView.getMeasuredHeight());
}
} else {
mVideoView.setVisibility(View.INVISIBLE);
mImageContainer.setVisibility(View.VISIBLE);
}
mPreviewThumbIcon.setVisibility(View.INVISIBLE);
return true;
In addition, put mediaplayer. Start into the custom preparedlistener
private class CustomMediaPlayer extends MediaPlayer implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (mediaPlayer != null) {
mediaPlayer.start();
}
}
//We start the video when its available
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.i(TAG,"Video Prepare");
mediaPlayer.start();
}
}
Hope to help you. Good luck!