Playing local mp3 file on Android?

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;


…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

is there any error in device log?

whats your url to load locally?

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, /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

local urls need to start with file:// or file:///

1 Like

mgear, big thanks, that fixed it.

Solution:

string fullPath = Application.persistentDataPath + “/” + filename;
fullPath = “file:///” + fullPath;

1 Like

so how actual to load .mp3 file on android or mobile?

Was getting a “Malformed URL” error with fullPath = “file:///” + fullPath;
Changing it to: fullPath = “file://” + fullPath;
Fixed the issue

1 Like