MediaPlayer 在第一次运行时不工作

Posted

技术标签:

【中文标题】MediaPlayer 在第一次运行时不工作【英文标题】:MediaPlayer is not working on the first run 【发布时间】:2016-06-23 02:54:40 【问题描述】:

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

    

这是我的自定义媒体播放器和我在初始化表面时调用的事件:

   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(),或者始终使用“可见”视图,但我找不到它不起作用的原因。

【问题讨论】:

【参考方案1】:

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


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

【讨论】:

以上是关于MediaPlayer 在第一次运行时不工作的主要内容,如果未能解决你的问题,请参考以下文章

UIButton 在第一次运行时工作正常,但在模拟器上第二次运行时不起作用

Caused by:java.lang.IllegalStateException at android.media.MediaPlayer._setDataSource(Native Method)

MediaPlayer不使用本地文件

Mediaplayer响应第二次点击暂停

Contact Form 7 在第一次提交时不起作用,但在第二次按下 sumbit 按钮时它工作正常

第一次运行插入查询由于 E 列而出现重复,并且在运行超过一次时不应插入重复记录