Search Unity

Audio How To Load A Local Mp3 File From The Android File System Into An Audioclip?

Discussion in 'Audio & Video' started by konstantin_lozev, Apr 10, 2019.

  1. konstantin_lozev

    konstantin_lozev

    Joined:
    May 8, 2015
    Posts:
    99
    I want for my game to allow the player to play their own mp3 in the background while playing. I read that in the past it was done with the www class, which is now deprecated. We should use instead UnityWebRequestMultimedia.GetAudioClip https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestMultimedia.GetAudioClip.html

    However, the example there is for a web-hosted audio file and not for a local file on the Android system and I have 2 unknowns on how to change it to fit my needs:

    • would the "yield returnwww.Send();" in the coroutine work for a local file path?

    • How should the file path on Android look like?
    Thanks in advance.
     
  2. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    it would
    but don't use .Send, just yield the request:
    Code (CSharp):
    1. yield return www;

    put your file into streaming assets and find it then by its name:
    Code (CSharp):
    1. Path.Combine(Application.streamingAssetsPath, "your-file-name.mp3");
     
  3. konstantin_lozev

    konstantin_lozev

    Joined:
    May 8, 2015
    Posts:
    99
    My use case is that the user would have to choose a file from their own Android file system. However, before building that functionality (by tweaking that asset https://assetstore.unity.com/packages/tools/input-management/file-selection-dialogue-11374) I want to test it with a given string for the file path. I don't know how the file path in Android should look like though, i.e. how is the Android root folder referenced from within Unity?
     
  4. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    the application has to have reading rights for given file first of all, I have no idea what the asset you linked does, but it should return path to the file - if not, you have to find workaround with that asset
    you can reference the file by its normal filesystem path, e.g. something like "/sdcard/media/music/file.mp3" would work with webrequest with file:// protocol
     
  5. konstantin_lozev

    konstantin_lozev

    Joined:
    May 8, 2015
    Posts:
    99
    Thanks, so the root folder for the sdcard should be "sdcard" and the uri for the SD card should be "file://sdcard/..."? What would the be for the built-in memory then? Or is it specific to the android device?
     
  6. alistairlacey15

    alistairlacey15

    Joined:
    Feb 18, 2018
    Posts:
    1
    Hi, did you find a solution as I have the same issue.

    Thanks
     
  7. RukiPomidory

    RukiPomidory

    Joined:
    Jun 14, 2018
    Posts:
    1
    Westland likes this.