1

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);
DAIRAV
  • 723
  • 1
  • 9
  • 31

1 Answers1

1

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++;
      });

}
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • This your method makes sense, but the problem is my app needs to download a number of videos in the background before i can play it for the user offline so i cant actually play it immediately after download, my updates question would be if Util.downloadUrlToFile(url, f, true) returns false does it delete the file immediately or how can i delete the incomplete downloaded file – IfeFromKolopay Jul 31 '18 at 10:29
  • 1
    It doesn't delete the file, you would need to delete it. As I said it's a simplified sample but downloading multiple elements in the background is pretty easy too. I chose the simplified version of download but you can use `ConnectionRequest` directly and download multiple files to different names as you see fit. Alternatively you can play a file directly from an HTTP/S URL. Notice that the HTTPS URL might fail in the simulator and the HTTP URL might fail on iOS devices... – Shai Almog Aug 01 '18 at 04:29