android-MediaPlayer第一次不工作

android-MediaPlayer第一次不工作,第1张

概述我在我的应用程序中添加了MediaPlayer.它可以正常工作,但是第一次调用表面并初始化MediaPlayer时,纹理变为可见,但纹理白色,没有视频(但声音正常,我的日志说它正常工作).第一次尝试后,它可以完美运行.但是我找不到它第一次尝试不起作用的原因.这是我的代码://///////mVideo

我在我的应用程序中添加了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 {@OverrIDepublic voID onCompletion(MediaPlayer mediaPlayer) {    if (mediaPlayer != null) {        mediaPlayer.start();    }}//We start the vIDeo when its available@OverrIDepublic voID onPrepared(MediaPlayer mediaPlayer) {    Log.i(TAG,"VIDeo Prepare");    mediaPlayer.start();}}

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

总结

以上是内存溢出为你收集整理的android-MediaPlayer第一次不工作全部内容,希望文章能够帮你解决android-MediaPlayer第一次不工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1120642.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存