mrmaffen / vlc-android-sdk

VLC Android SDK pushed to Maven Central. Primarily used in project tomahawk-android.
792 stars 244 forks source link

Update surfaceview when replaying video #78

Open nguyenhoanglam opened 7 years ago

nguyenhoanglam commented 7 years ago

When media player reach end of video, I call release method like this:

  public void release() {
        if (mLibVLC == null)
            return;
        final IVLCVout vout = mMediaPlayer.getVLCVout();
        vout.removeCallback(this);
        vout.detachViews();

        mLibVLC.release();
        mLibVLC = null;

        mVideoWidth = 0;
        mVideoHeight = 0;
    }

and then create a new media player to replay the video:

   private void createPlayer(String url) {
        try {
            ArrayList<String> options = new ArrayList<>();
            options.add("--aout=opensles");
            options.add("--audio-time-stretch");
            options.add("--http-reconnect");
            options.add("--avcodec-hw=vaapi");
            options.add("--network-caching=1500");
            options.add("-vvv");
            mLibVLC = new LibVLC(getContext(), options);

            mMediaPlayer = new MediaPlayer(mLibVLC);
            mMediaPlayer.setEventListener(mPlayerListener);

            final IVLCVout vout = mMediaPlayer.getVLCVout();
            vout.setVideoView(mSurfaceView);
            vout.addCallback(this);
            vout.attachViews();
        } catch (Exception e) {
            LogUtils.show("Error creating player: " + e.getMessage());
        }
    }

``` java
@Override
    public void onSurfacesCreated(IVLCVout vlcVout) {
        Media media = new Media(mLibVLC, Uri.parse(mRecordVideo.getLinkAndroid()));
        media.setHWDecoderEnabled(true, true);
        mMediaPlayer.setMedia(media);
        mMediaPlayer.play();
    }

The problem is: although the media player is streaming correctly, the surfaceview still not displaying the video. I don't know why the surfaceview did not update video frame. Please help me!

SaundersB commented 7 years ago

Make sure onSurfacesCreated is actually being called. I ran into a similar issue as well. If onSurfacesCreated is never called, your media will never begin playing.

Also you don't necessarily need to release the player. You could just load a new Media object, set the media to the player, and then play the player.

nguyenhoanglam commented 7 years ago

@SaundersB Thanks for your guide. I'll try it now. By the way, VLC Android SDK still have some issues (stop MediaPlayer cause app freezed...)