Search code examples
androidmedia-player

How to skip missing chunks in android when playing video?


I'm writing an android app that accepts an HLS and shows via VideoView. But sometimes there are missing chunks in the stream, and my player crashes. Here's how I'm trying to fix it.

player.setOnErrorListener(playerErrorHandler);

private class PlayerErrorHandler implements MediaPlayer.OnErrorListener {

    private boolean handledError = false;

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        if (!handledError) {
            if (!isOnline()) {
                finish();
            }
            //if we're here then we have a missing chunk
            int currentPosition = mp.getCurrentPosition();
            // check if seekForward time is lesser than song duration
            mp.seekTo(currentPosition + 10);

        } else {
            handledError = false;
        }
        return true;
    }
}



private boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

So i just check on error if there's nothing wrong with my connection, and if erevything's ok, it means i have a missing chunk in my stream. But the problem is, my forwarding doesn't work and it keeps throwing an error continuously. How do I forward like 10 seconds? Or maybe better, is there a way to tell the mediaplayer to automatically skip missing chunks and continue playing?


Solution

  • got the right way through trials and errors. Here's how i skip the missing chunks

    changed these lines

     int currentPosition = mp.getCurrentPosition();
     mp.seekTo(currentPosition + 10);
    

    to these

     int currentPosition = mp.getCurrentPosition();
    
     player.setVideoPath(streamUrl);
     player.seekTo(currentPosition + 5000);
     player.start();