androidandroid-mediaplayerandroid-textureview

android check if mediaplayer finished in TextureView


i have one TextureView.i can play video from assess folder in my TextureView. now i want to check if video is finished.i wrote setOnCompletionListener but not working.this is a my source

 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) {

        }
    });
}

i also wrote setOnCompletionListener listener outside the setSurfaceTextureListener but also not working. how i can solve my problem? if anyone knows solution please help me


Solution

  • Most likely, your OnCompletionListener is never called because of the following line:

    mMediaPlayer.setLooping(true);
    

    According to the 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.

    If you want to get a callback from the MediaPlayer when it completes playback and also loop playback, you could change your implementation to follow the suggestion given in the third bullet point: remove mMediaPlayer.setLooping(true) and in your OnCompletionListener call mp.start() to restart playback from the beginning.