0

I wanna playback some audiofiles in a UniversalWindowsApp to those I've only got the path. This is my current code:

//exception in this line:
var file = await StorageFile.GetFileFromPathAsync(@"C:\Users\myuser\Music\someartist\album\01. Title.mp3");

var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
mediaControl.SetSource(stream, file.ContentType);
mediaControl.Play();

I also tried this code snippet:

mediaControl.Source = new Uri(@"C:\Users\myuser\Music\someartist\album\01. Title.mp3", UriKind.RelativeOrAbsolute);

But both codes throw an exception as follows:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Which is not really located at the first one (means that it then immediately jumps to the UnhandledException), as it breaks to the middle line (copied from App.g.i.cs/InitializeComponent()):

UnhandledException += (sender, e) =>
{
    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};

If I use the sample code from msdn it works:

async private void SetLocalMedia()
{
    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
    openPicker.FileTypeFilter.Add(".mp3");
    var file = await openPicker.PickSingleFileAsync();

    // mediaControl is a MediaElement defined in XAML
    if (null != file)
    {
        var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        mediaControl.SetSource(stream, file.ContentType);
        mediaControl.Play();
    }
}

Which does obviously the same as mine expect the FileOpenPicker - a dialog I won't show up as I already have the filepath.

The question is, how can I playback some audio in a UniversalWindowsApp?

Thanks in advance!

cramopy
  • 3,459
  • 6
  • 28
  • 42
  • the exception clearly says you don't have access to that file. For `File.Open` or `FileStream` you have various options to open a file which may provide right access but the so-called `StorageFile.GetFileFromPathAsync` does not have any option. That means you cannot stick to that method. – Hopeless Oct 04 '15 at 15:46

1 Answers1

0

You need to know about file access and permissions in Windows Store Apps https://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx

Mbithy Mbithy
  • 138
  • 13