Search code examples
javascriptwin-universal-appwinjsfileopenpicker

Winjs project - use File picker to select audio file and play it


I'm working with Visual Studio 2017, winjs project. How to use FilePicker to open file dialog and pick an audio file, then play that file? Thanks guys.


Solution

  • You could use the Fileopenpicker in your JS code like this:

    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
        openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
        openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
        // Users expect to have a filtered view of their folders depending on the scenario.
        // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
        openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
    
        // 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 photo: " + file.name, "sample", "status");
            } else {
                // The picker was dismissed with no selected file
                WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
            }
        });
    

    What you need is to change the file type to the target type you need.

    There is a JS sample that you could directly refer to: FilePicker - JS.