我在我的应用中添加了一个MediaPlayer。它工作正常,但我第一次调用我的表面,我初始化MediaPlayer纹理变得可见,但它的白色,没有视频(但声音正在工作,我的日志说它工作正常)。
第一次尝试后,它完美无缺。但是我找不到它第一次尝试不起作用的原因。这是我的代码:
/////////
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) {
}
这是我的自定义MediaPlayer以及我在表面处理时调用的事件:
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();
}
}
最后我在onDragListener中调用start():
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;
我已经尝试更改prepareAsync()的prepare(),或者总是使用“可见”视图,但我找不到它无效的原因。
非常感谢你!
答案 0 :(得分:3)
每次在Drop&内部使用后重置MediaPlayer。拖动方法:Action_drag_ended:
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;
另外,将Mediaplayer.start放入自定义的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();
}
}
希望它会对你有所帮助。祝你好运!