Cordova 3.4 Android Local Video files not playing

南笙酒味 提交于 2019-12-03 04:03:11
mab

Take a look at this post. The File plugin (at least <=v1.3.1) has a bug for android devices. Also, I am not sure if jaeger25/Html5Video plugin is still working with cordova 3.6.x.

A working approach is to programmatically copy your video files from www/gruppenruf.mp4 to a place accessible for playback by the app during runtime. You may use file:///data/data/com.example.MyPackage/files/gruppenruf.mp4 for that. The FileTransfer cordova plugin will take care of this.

var myFilename = "gruppenruf.mp4";
var myUrl = cordova.file.applicationDirectory + "www/" + myFilename;
var fileTransfer = new FileTransfer();
var filePath = cordova.file.dataDirectory + myFilename;

fileTransfer.download(encodeURI(myUrl), filePath, (function(entry) {
  /*
  res = "download complete:\n"
  res += "fullPath: " + entry.fullPath + "\n"
  res += "localURL: " + entry.localURL + "\n"
  alert(res += "nativeURL: " + entry.nativeURL + "\n")
   */
  var vid = document.getElementById("myvideo");
  vid.src = entry.nativeURL;
  //vid.loop = true;
}), (function(error) {
  alert("Video download error: source " + error.source);
  alert("Video download error: target " + error.target);
}), true, {
  headers: {
    Authorization: "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
  }
});

If it does not play you may want to link a click event listener to it or start the video with vid.play();.

If the mentioned options are not working try:

src="android.resource://com.example.hello/raw/Videofile" (leave the extension ".mp4" what so ever)

place your video files in "..\platforms\android\res\raw" note that you don't use the extension of the video file in the src.

I've struggled a lot with this issue, but this worked for me =]

As I spend some time with a similar issue, I feel its worth sharing:

  1. If you want to display video fullscreen: dont use html tag as there is a media plugin for that. If you dont need to play offline: the video src pointing to a web server will work just fine.
  2. If you need to play offline and the video is part of your app package, have a look to https://github.com/jaeger25/Html5Video (it help to automatically copy the video files in a public location)
  3. If you need to play offline videos dynamically downloaded: (my case)
    • Use fileTransfer plugin
    • Save file in cordova.file.externalDataDirectory as its public
    • In some case you may need to force the file to be public
    • you can do that by adding 'file.setReadable(true, false);' to FileTransfer.java line 820

Cheers

Your code stopped working in Cordova 3.4.0 because they changed the file path structure. Instead of 'file:///' you have to use 'cdvfile://'.

Remember to update the File Plugin as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!