gpt4 book ai didi

android - MediaPlayer 在第一次运行时不工作

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:17 25 4
gpt4 key购买 nike

我在我的应用程序中添加了一个 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;

我已经尝试将我的 prepare() 更改为 prepareAsync(),或者始终使用“可见” View ,但我找不到它不起作用的原因。

最佳答案

在您的拖放方法中每次使用后重置您的 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();
}
}

希望对您有所帮助。祝你好运!

关于android - MediaPlayer 在第一次运行时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com