问题
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);
回答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++;
});
}
来源:https://stackoverflow.com/questions/51592056/codenameone-mediaplayer-generates-cant-play-this-video-error-on-android-how-d