1

I have a recorded audio stored in an USB storage and list them in the listbox. I would like to select them and click 'play' to play individual file respectively. Currently I manage to read the filename but unable to play the file.

My code as below.

Updated: (recordlist is the name for the listbox)

private async void recordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          recordList.SelectedItem = recordIndex;
          string recordFileName = recordList.SelectedItem.ToString();
          StatusMessage.Text = recordFileName;

          StorageFolder externalDevices = KnownFolders.RemovableDevices;
          IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
          StorageFolder usbStorage = externalDrives[0];
          StorageFolder recordFolder = await usbStorage.CreateFolderAsync(recFolderName, CreationCollisionOption.OpenIfExists);
          await usbStorage.GetFolderAsync(recFolderName);
          StorageFile recordFile = await usbStorage.GetFileAsync(recordFileName);

          recordPlayer.Source = Windows.Media.Core.MediaSource.CreateFromStorageFile(recordFile);
          recordPlayer.AutoPlay = false;
        }

This is the code is use to display all the .mp3 files in my USB drive on the listbox.

private async void displayRecord()
    {
        StorageFolder externalDevices = KnownFolders.RemovableDevices;
        IReadOnlyList<StorageFolder> externalDrives = await externalDevices.GetFoldersAsync();
        StorageFolder usbStorage = externalDrives[0];

        StorageFolder recordFolder = await usbStorage.CreateFolderAsync(recFolderName, CreationCollisionOption.OpenIfExists);
        await usbStorage.GetFolderAsync(recFolderName);
        IReadOnlyList<StorageFile> recFileList = await recordFolder.GetFilesAsync();

        foreach(StorageFile file in recFileList)
        {
            recordList.Items.Add(file.Name);
        }

    }
mylim
  • 313
  • 4
  • 16
  • What is the meaning of your variable **recordIndex**?An integer index of your list selected index?Your code showed above is in what function?Please give me more detail information. – Michael Xu Sep 29 '17 at 01:42
  • What is the value of variable **recordIndex** when you selected an item in the list?You can debug and watch the recordIndex and recordFileName. – Michael Xu Sep 29 '17 at 02:00
  • Hi Micheal, I have added the detailed code above. Yes, recordindex is index of the selected file in the listbox. I managed to list all the .mp3 files from USB storage & select them & read out their respective file names and display on a textblock. However, I didn't manage to load them to be played by my recordPlayer (MediaPlayer). – mylim Sep 29 '17 at 02:02

1 Answers1

2

The problem is due to wrong way to get the selected file, you should get the file from your recordFolder instead of usbStorage.

await usbStorage.GetFolderAsync(recFolderName);

Although you get the record folder,you did not redirect to this folder to get file.

StorageFile recordFile = await usbStorage.GetFileAsync(recordFileName);

In this line code ,you get the file from usb root folder or not your record folder, of course will not get the file, there will be an exception thrown.

Please see below code in recordList_SelectionChanged function I modified,it runs ok.

      //await usbStorage.GetFolderAsync(recFolderName);
      //StorageFile recordFile = await usbStorage.GetFileAsync(recordFileName);
      recordFolder = await usbStorage.GetFolderAsync(recFolderName);
      StorageFile recordFile = await recordFolder.GetFileAsync(recordFileName);
Michael Xu
  • 4,382
  • 1
  • 8
  • 16