android 检查媒体播放器是否在 TextureView 中完成

android check if mediaplayer finished in TextureView

我有一个 TextureView.i 可以播放我 TextureView 中评估文件夹中的视频。 现在我想检查视频是否 finished.i 写了 setOnCompletionListener 而不是 working.this 是我的来源

 private void playVideoAnimation()
{
    textureView.setVisibility(View.VISIBLE);
    mMediaPlayer = new MediaPlayer();

    textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
            Surface surface = new Surface(surfaceTexture);

            try {
                Log.e("FILE_NAME",FILE_NAME);
                AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);

                mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                mMediaPlayer.setSurface(surface);
                mMediaPlayer.setLooping(true);
                mMediaPlayer.prepareAsync();
                mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mediaPlayer.start();
                    }
                });

                mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {

                        Log.e("FILE_NAME", "Finished");
                    }

                });



            } catch (IllegalArgumentException e) {
                Log.d(TAG, e.getMessage());
            } catch (SecurityException e) {
                Log.d(TAG, e.getMessage());
            } catch (IllegalStateException e) {
                Log.d(TAG, e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, e.getMessage());
            }
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {

            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {

        }
    });
}

我还在 setSurfaceTextureListener 之外编写了 setOnCompletionListener 侦听器,但也无法正常工作。 我该如何解决我的问题?如果有人知道解决方案请帮助我

很可能,您的 OnCompletionListener 从未被调用,因为以下行:

mMediaPlayer.setLooping(true);

根据 MediaPlayer documentation:

When the playback reaches the end of stream, the playback completes.

  • If the looping mode was being set to true with setLooping(boolean), the MediaPlayer object shall remain in the Started state.
  • If the looping mode was set to false, the player engine calls a user supplied callback method, OnCompletion.onCompletion(), if a OnCompletionListener is registered beforehand via setOnCompletionListener(OnCompletionListener). The invoke of the callback signals that the object is now in the PlaybackCompleted state.
  • While in the PlaybackCompleted state, calling start() can restart the playback from the beginning of the audio/video source.

如果您想在 MediaPlayer 完成播放时获得回调 并且 还循环播放,您可以更改您的实现以遵循中给出的建议第三个要点:删除 mMediaPlayer.setLooping(true) 并在 OnCompletionListener 中调用 mp.start() 从头开始​​播放。