音频显示使用文件打开选取器链接到本地计算机上的文件时'Invalid source'



我想写一个JS Windows应用程序。我在第一页上有一个按钮,点击事件处理程序代码如下:

函数pickSingleAudioFile(args) {. getelementbyid("输出")。innerText += "n" + this。

    // Create the picker object and set options
    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
    openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
    openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.musicLibrary;
    openPicker.fileTypeFilter.replaceAll([".mp3"]);
    // Open the picker for the user to pick a file
    openPicker.pickSingleFileAsync().then(function (file) {
        if (file) {
            // Application now has read/write access to the picked file
            WinJS.log && WinJS.log("Picked file: " + file.name, "sample", "status");
            document.getElementById("output").innerText += "You picked " + file.name + " from " + file.path;
            // Save the file as an audio tag and load it
            var audtag = document.createElement('audio');
            audtag.setAttribute("id", "audtag");
            audtag.setAttribute("controls", "true");
            audtag.setAttribute("msAudioCategory", "backgroundcapablemedia");
            audtag.setAttribute("src", """ + file.path + """);
            document.getElementById("output").appendChild(audtag);
            audtag.load();
        } else {
            // The picker was dismissed with no selected file
            WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
        }
    });
}

路径是类似"D:Songssong .mp3"或"network-shareMy Musicsong name.mp3"我得到"Invalid Source"错误时试图加载文件。

乍一看,

audtag.setAttribute("src", """ + file.path + """);

应该是这样:

audtag.setAttribute("src", file.path);

不清楚为什么要添加反斜杠。但是,根据您正在做的事情和我所看到的示例,您最好这样做:

var fileLocation = window.URL.createObjectURL(file, { oneTimeOnly: true });
audtag.setAttribute("src", fileLocation);

你可以从Windows开发中心查看"播放管理器msAudioCategory样本"以获得更多的想法。

相关内容

最新更新