其他分享
首页 > 其他分享> > android-MediaPlayer第一次不工作

android-MediaPlayer第一次不工作

作者:互联网

我在我的应用程序中添加了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) {

    }

这是我的Custom 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(),或始终使用“可见”视图,但是我无法找到其不起作用的原因.

提前非常感谢您!

解决方法:

每次在Drop& amp;内部使用后,请重置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();
}
}

希望对您有帮助.祝好运!

标签:drag-and-drop,android-camera,surfaceview,media-player,android
来源: https://codeday.me/bug/20191118/2032213.html