locked
Playing Local Music File from Music Directory in Windows Store apps HTML-JavaScript using Audio-tag RRS feed

  • Question

  • Hi All,

    I have audio tag in my Windows Store application and I want to play music from Music Directory of the Device.

    Following is the src I am setting for Audio Tag

    audioTag.src = "file:///C:/Users/Ashwin/Music/JiveHD Songs/Yi Qie Wei Liao Ai.mp3"

    But It audioTag onError event is raised.

    Can any one help me with this?

    Monday, July 29, 2013 8:51 AM

Answers

  • The file:// protocol is not allowed for URIs in this context. Other than http[s], the other allowed protocols are ms-appx(in-package contents) and ms-appdata(appdatacontents). To refer to something in a library will require a StorageFileobject that you can pass to URL.createObjectURL.

    If you have the Music library capability checked in your manifest (which is required here), you can get a StorageFile from a path using the static method StorageFile.getFileFromPathAsync, which works with absolute paths. More appropriately, though, is to get the StorageFolder for the music library from Windows.Storage.KnownFolder.musicLibrary, then use it's getFileAsync method with the relative path ("JiveHD Songs/Yi Qie Qei Liao Ai.mp3" in your case). Typically you'd be using this latter method, as an app would be looking at the music library of the current user. I figure the example you give is just a quick test.

    Anyway, assuming that the variable "file" has the StorageFile, you'll then assign audioTag.src = URL.createObjectURL(file).

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press

    Also see second edition preview


    Tuesday, July 30, 2013 5:14 PM

All replies

  • The file:// protocol is not allowed for URIs in this context. Other than http[s], the other allowed protocols are ms-appx(in-package contents) and ms-appdata(appdatacontents). To refer to something in a library will require a StorageFileobject that you can pass to URL.createObjectURL.

    If you have the Music library capability checked in your manifest (which is required here), you can get a StorageFile from a path using the static method StorageFile.getFileFromPathAsync, which works with absolute paths. More appropriately, though, is to get the StorageFolder for the music library from Windows.Storage.KnownFolder.musicLibrary, then use it's getFileAsync method with the relative path ("JiveHD Songs/Yi Qie Qei Liao Ai.mp3" in your case). Typically you'd be using this latter method, as an app would be looking at the music library of the current user. I figure the example you give is just a quick test.

    Anyway, assuming that the variable "file" has the StorageFile, you'll then assign audioTag.src = URL.createObjectURL(file).

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press

    Also see second edition preview


    Tuesday, July 30, 2013 5:14 PM
  • Hi Kraig,

    If I develop using c#,xaml still I will face the same problem or not?

    Can I directly pass local file path to MediaElement?

    Tuesday, September 10, 2013 6:24 AM
  • Generally speaking, all apps are subject to the same restrictions on file access, e.g. getFileFromPathAsync requires that you already have programmatic access to that location. This is a characteristic of WinRT, not of the language you're using.

    Tuesday, September 10, 2013 2:51 PM