- Home /
Trouble getting to DIRECTORY_MUSIC in Android
Greetings all, I'm looking for the right method to access an Android devices Music folder (for read access only)
I've read numerous posts on how this can be done through Java, but as a novice coder I'm having a tough time integrating that into a C script.
Unity 5.3.4 personal (free)
Android (presuming 5.0 Lollipop)
Ideally I'd like to get Environment.DIRECTORY_MUSIC returned as a string for processing since that unmarries me from doing it manually but it looks like I need to use a Java class to get to it?
Example below works for Windows using Application.datapath, but even set manually (Note 3, checked the file structure manully) it fails. The end goal is to have this on Android, an d a simple pull of the "Music" folder directory list to an array, and the location stored as a string (androidPath).
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class MusicManager : MonoBehaviour {
public static string[] fileinfo;
public static int Clipcount;
public static string androidPath = "/storage/extSdCard/Audio/";
public string = AndroidJavaObject Environment.DIRECTORY_MUSIC;
// Use this for initialization
void Start()
{
SetGetFiles();
}
// Update is called once per frame
void Update()
{
}
public static void SetGetFiles()
{
// pc version on top works
// var info = Application.dataPath + "/Audio/";
var info = "file:///" + androidPath;
var ftype = "*.mp3";
fileinfo = Directory.GetFiles(@info, ftype);
Clipcount = fileinfo.Length;
}
}
Best answers all seem to be making a Javascript wrapper to use for querying this element. So far nobody has been able to point me to a C# or Unity based solution. In the meantime, direct access to that folder is available, in 1 of 2 "Android default" locations, one for devices with internal storage only at "/storage/emulated/0/$$anonymous$$usic" and one for devices with an 'external' SD Card at "/storage/extSdCard/$$anonymous$$usic" This seems to be valid for multiple generations of Samsung devices, and each of the Nexus' I have tested with. Hope it helps!