Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access phone music library

Discussion in 'Windows' started by LaurensiusTony, Jun 12, 2014.

  1. LaurensiusTony

    LaurensiusTony

    Joined:
    May 22, 2013
    Posts:
    78
    Hi, i'm making game that will generate the level and enemy behaviour according to the music spectrum data and i want user can select their own music from their phone to be played in the game... so far i read there is no documentation about that for wp unity, i found iphone and android but it won't work since my target device is windows phone...

    so maybe anyone ever tried it? or maybe have a tutorial to read all music in the phone library and let user select which one they want to my level audiosource?
     
  2. pumpkinszwan

    pumpkinszwan

    Joined:
    Feb 6, 2014
    Posts:
    214
    I would start by checking if this info is available to Windows Phone apps via the SDK. Look on dev.windowsphone.com.
    If it's possible you will probably have to use one of the techniques for accessing Windows Phone APIs from Unity scripts.
     
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,491
    Hi,

    this should be pretty trivial to do with Windows Phone API. You can get StorageFolder class of users music folder with this API:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.knownfolders.musiclibrary

    From it, you can query its path and then you can enumerate files/make Unity load them. Note, you will have to enable musicLibrary permission in the manifest, and you'll only be able to use this API in a plugin (not your scripts directly). Here's a guide how to create a plugin yourself:

    https://docs.unity3d.com/Documentation/Manual/wp8-plugins-guide-csharp.html
     
  4. LaurensiusTony

    LaurensiusTony

    Joined:
    May 22, 2013
    Posts:
    78
    thank you for pointing out, but i really have no idea how to make that.... i also searching around and find some piece of code that maybe can be assosiated for windows phone, but it's turned not working maybe there is something wrong?

    well it's in javascript

    Code (JavaScript):
    1. var MusicFolder : System.IO.DirectoryInfo;
    2. var myClip : WWW;
    3. var myPath : String;
    4. function Start()
    5. {
    6.     myPath = "/mnt/sdcard/music";
    7.     MusicFolder = new System.IO.DirectoryInfo(myPath);
    8.     myClip = new WWW("file:///" + MusicFolder.GetFiles()[0].FullName);
    9.     audio.clip = myClip.GetAudioClip(false, false);
    10. }
    11. function Update()
    12. {
    13.     if (!audio.isPlaying && audio.clip.isReadyToPlay){
    14.         soundPlayer.audio.Play();
    15.     }
    16. }
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,627
    You should create a plugin for this. You can not use Windows Phone APIs in Unity scripts and this is even more true for JavaScript.
     
  6. shweta-unity

    shweta-unity

    Joined:
    Nov 23, 2016
    Posts:
    7
    Hi,
    Is there any way i can get list of all music files except for any specific path as mentioned?
    Because i want all the music files available at any folder of device to list and get it's path
    (i am working for iOS and android)
    Any help would be great..

    Code (JavaScript):
    1. var MusicFolder : System.IO.DirectoryInfo;
    2. var myClip : WWW;
    3. var myPath : String;
    4. function Start()
    5. {
    6.     myPath = "/mnt/sdcard/music";
    7.     MusicFolder = new System.IO.DirectoryInfo(myPath);
    8.     myClip = new WWW("file:///" + MusicFolder.GetFiles()[0].FullName);
    9.     audio.clip = myClip.GetAudioClip(false, false);
    10. }
    11. function Update()
    12. {
    13.     if (!audio.isPlaying && audio.clip.isReadyToPlay){
    14.         soundPlayer.audio.Play();
    15.     }
    16. }