Cordova 无法在 Windows 10 上播放文件

Cordova Cannot Play file on Windows 10

我使用的是最新版本的 Cordova + VS2015。

当我尝试在 windows10 应用程序中播放作为应用程序一部分包含的 mp3 文件时,出现错误(代码:1)

在 iOS 和 Android 下 运行 时,游戏运行良好,似乎只有 windows 10 是问题所在。

我在路径“/myfile.mp3”、"myfile.mp3"、“/www/myfile.mp3”、"www/myfile.mp3" 上尝试了多种变体,结果都相同。我尝试了一个不存在的文件名并得到了同样的错误。这使我相信路径不正确。如果我打印 window.location.pathname,我得到“/www/index.html”,因此,我怀疑“/www/myfile.mp3”应该有效。

该文件已添加到项目中,它显示在 platforms 文件夹的 www 目录中。

这是代码,基本上是从手册中截取的

    console.log(window.location.pathname);
    var myMedia = new Media('/www/myfile.mp3', function () {
            console.log('success');
    }, function (e) {
            console.log(e); // I always land here, where is is 'code: 1'
    }, function (e) {
            console.log(e);
    });
    myMedia.play();

使用"ms-appx:"方案即可解决问题。请参阅 document 的应用程序包部分:

To access files stored inside the application package, but from code where there is no inferred root authority, specify the ms-appx: scheme.

在你的情况下,路径应该修改为"ms-appx:///www/myfile.mp3",如果当前平台是windows(这里需要安装cordova-plugin-device 作为准备工作):

var url = "";
if (device)
{
    if (device.platform.toLowerCase() == "windows") {
        url = "ms-appx:///www/myfile.mp3";
    } else {
        url = "/myfile.mp3";
    }
}
var myMedia = new Media(url, function () {
    console.log('success');
}, function (e) {
    console.log(e); // I always land here, where is is 'code: 1'
}, function (e) {
    console.log(e);
});
myMedia.play();