嗯,我很难过。
使用cordova 6.1.1我构建了一个vanilla app并添加了Media和File Transfer插件。然后,我在File Plugin CDVFile documentation中描述了代码,试图让最小的javascript从外部位置下载MP3并播放它。所以我用以下内容替换了默认的index.js:
var onDeviceReady = function() {
var fileURL = 'cdvfile://localhost/temporary/test.mp3'
var uri = 'https://www.dropbox.com/s/1mg3pqfmkuc5g4p/Mad%20Dad%27s%20Song%20v2%20TRACK.mp3?dl=1'
var onSuccessfulDownload = function (entry) {
console.log("Successfully downloaded to: " + entry.toURL());
var my_media = new Media(fileURL, function(){
console.log("Successfully loaded");
alert("Successfully loaded");
}, function(err) {
console.log("Failed to load" + JSON.stringify(err));
alert("Failed to load" + JSON.stringify(err));
});
my_media.play();
}
var ft = new FileTransfer();
ft.download(encodeURI(uri), fileURL, onSuccessfulDownload);
}
document.addEventListener('deviceready', onDeviceReady, false);
当我在我的Android设备上运行(真实,未模拟)时,文件会成功下载(" Successfully downloaded to: file:///data/data/com.example.media_test/cache/test.mp3
")但无法创建Media对象(& #34; Failed to load{"code":1}
&#34)。使用adb logcat
我可以看到MediaPlayer尝试加载文件,但错误我们不是非常冗长:
V/MediaPlayer(15167): setListener
V/MediaPlayer-JNI(15167): setDataSourceFD: fd 151
V/MediaPlayer(15167): setDataSource(151, 0, 576460752303423487)
V/MediaPlayerService( 189): Create new client(106) from pid 15167, uid 10228,
V/MediaPlayerService( 189): setDataSource fd=40, offset=0, length=576460752303423487
V/MediaPlayerService( 189): st_dev = 45848
V/MediaPlayerService( 189): st_mode = 33152
V/MediaPlayerService( 189): st_uid = 10228
V/MediaPlayerService( 189): st_gid = 10228
V/MediaPlayerService( 189): st_size = 170536
V/MediaPlayerService( 189): calculated length = 170536
V/MediaPlayerService( 189): player type = 3
...snip ...
W/MediaExtractor( 189): FAILED to autodetect media content.
E/MediaPlayerService( 189): error: -2147483648
... snip ...
E/MediaPlayer(15167): Unable to create media player
W/PluginManager(15167): THREAD WARNING: exec() call to Media.startPlayingAudio blocked the main thread for 33ms. Plugin should use CordovaInterface.getThreadPool().
... snip ...
I/chromium(15167): [INFO:CONSOLE(11)] "Failed to load{"code":1}", source: file:///android_asset/www/js/index.js (11)
/android_asset/www/test.mp3
来播放它,所以我知道文件本身本身并不存在问题<access origin="cdvfile://*"/>
添加到config.xml并将我的Content-Security-Policy设置为<meta http-equiv="Content-Security-Policy" content="* * 'self' default-src 'unsafe-inline' 'unsafe-eval' http://* https://* data: cdvfile://* content://*;">
,但无效。cdvfile://localhost/persistent/test.mp3
并不会产生影响有关如何调试此内容的任何提示?
修改
此外:
file://...
将路径传递到媒体并不会产生影响/android_asset/...
document.getElementById('foo').src = fileURL;
的内容在SuccessfulDownload上显示。因此,问题似乎并不是关于阅读和写作,而是专门关于打开MP3文件。答案 0 :(得分:0)
据我所知,您无法将文件写入应用程序的缓存目录,直到设备生根为止。所以我怀疑你是否真的能够使用任何文件浏览器查看相应目录中的文件。
您可以尝试使用cordova.file.externalRootDirectory或cordova.file.externalRootDirectory。此外,如果你在Android Marshmallow上测试它,那么你必须使用最新版本的cordova文件插件(4.2.0),因为旧版本似乎有Marshmallow的许可问题。希望它有帮助。