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

Playing local mp3 file on Android?

Discussion in 'Scripting' started by kevinjcourtney, Jan 19, 2020.

  1. kevinjcourtney

    kevinjcourtney

    Joined:
    Apr 29, 2018
    Posts:
    3
    Hello,

    I'm struggling to get my just downloaded local mp3 file to play on my Android, it works fine on my Windows Unity development computer.

    My app:
    - Downloads and plays an MP3 from my dropbox location
    - It then saves the Mp3 to the Application.persistentDataPath
    - Next time playing it is selected in the app, it attempts to play the mp3 from the saved mp3 file
    - this works fine under Windows
    - but on Android, the app won't play the mp3.
    - I find the saved file on the Android outside of my app and can play it fine....which tells me it downloaded successfully

    To give permissions to the app, I've done this
    if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead)) {
    Permission.RequestUserPermission(Permission.ExternalStorageRead);
    }
    ...and can confirm at runtime that the above is successful

    and this in the manifest;
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    ...and this:
    android:usesCleartextTraffic="true"

    The code segment:
    (audioType is MPEG)
    using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(fullPath, audioType)) {
    yield return www.SendWebRequest();
    AudioClip myClip = DownloadHandlerAudioClip.GetContent(www);

    I've looked thru dozens of potential solutions online and no luck.

    Questions:
    - Can we play local Mp3 files from the Application.persistentDataPath on Android
    - if so, any ideas of what I'm missing?
    - If not, is it that we still have to convert the Mp3 to a WAV file in order to play the on Android? Or?

    Thanks in advance,
    Kevin
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,988
  3. kevinjcourtney

    kevinjcourtney

    Joined:
    Apr 29, 2018
    Posts:
    3
    Thanks for the quick reply....

    I'm using the same URL as when I saved it, which now that I look at it, might be the issue:

    /storage/emulated/0/Android/data/com.eAardvark.CalmerThanYouAreDude/files/TakeItEasy.mp3

    ...if it looks suspect and you know what it should be for the persistentDataPath, that will be appreciated.

    errors in log:

    2020-01-19 10:57:03.516 824-21261/? E/ResolverController: No valid NAT64 prefix (100, <unspecified>/0)
    2020-01-19 10:57:03.519 20969-21079/com.eAardvark.CalmerThanYouAreDude E/Unity: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:80

    Thanks! Kevin
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,988
    local urls need to start with file:// or file:///
     
    DhiaSendi likes this.
  5. kevinjcourtney

    kevinjcourtney

    Joined:
    Apr 29, 2018
    Posts:
    3
    mgear, big thanks, that fixed it.

    Solution:

    string fullPath = Application.persistentDataPath + "/" + filename;
    fullPath = "file:///" + fullPath;
     
    Westland likes this.
  6. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    660
    so how actual to load .mp3 file on android or mobile?
     
  7. DhiaSendi

    DhiaSendi

    Joined:
    May 16, 2018
    Posts:
    42
    Was getting a "Malformed URL" error with fullPath = "file:///" + fullPath;
    Changing it to: fullPath = "file://" + fullPath;
    Fixed the issue