Search code examples
javaandroidmedia-playercodenameone

Codenameone MediaPlayer generates 'can't play this video' error on Android how do i intercept the dialog so it fails silently


My codenameone project has a mediaPlayer which plays videos once they are downloaded, in some instances when the network breaks the video is still saved on the device even though it is corrupt and when the player tries to play this video i get the 'can't play this video' dialog. How can i intercept this so it doesnt show the dialog and i can handle the error in the background, my code for the media player is below

video = MediaManager.createMedia(videostream, "video/mp4", new Runnable() {
                                                @Override
                                                public void run() {

                                                    videoIsPlaying = false;
                                                    videoCounter++;

                                                }
                                            });



                                            video.setNativePlayerMode(true);

                                            newmp = new MediaPlayer(video);

                                            newmp.setAutoplay(true);
                                            newmp.hideControls();

                                            newmp.setHideNativeVideoControls(true);

Solution

  • I would separate this into two processes:

    • Download
    • Play

    You can download the video locally to file system, the play that file. By separating you can also create a pleasing progress animation during the download process and even provide indication of the download pace.

    A simplistic download/play process would look something like this:

    import static com.codename1.ui.CN.*;
    

    Then for playback/download:

    String f = getAppHomePath() + "videoName.mp4";
    if(Util.downloadUrlToFile(url, f, true)) {
        video = MediaManager.createMedia(f, () -> {
              videoIsPlaying = false;
              videoCounter++;
          });
    
    }