- Home /
Android Music selection
I am in the process of making an application for android using unity and I would like to be able to get a user selected song from their own library of music on their android into my unity application. I was wondering what would be the best way to go about this.
Is there an android specific script call that can bring up a song picker or return to me a list of the music stored on the device?
If there isn't, is there a simple way to bring up a file viewer like finder or explorer so that the user can look for the songs themselves and I can just use the path to load the song.
Or do I have to write a small song picking application in java that gets called from Unity?
Or have I overlooked a really simple way to do this?
Answer by WoozyBytes · Apr 13, 2015 at 05:03 PM
Hi there i know this is an old thread and you probably already found a solution for your problem but just in case someone is still interested, the way i approached this was to create my own file browser using .Net System.IO.
Check the Directory Class , all you need is there.
Start with Directory.GetCurrentDirectory() to get the current Directory path, on Android this will give you "/" on Windows when running in Editor will give you the path of where your project is.
Store the current Path in a string variable like this var _curPath : String = Directory.GetCurrentDirectory()
then feed that path variable into Directory.GetDirectories which will return you a list of the paths of all the folders (if any otherwise the length of the list will be zero ) in the path you just gave it. Store those paths in an string array i recommend using Generic Lists instead of unity's built in arrays simply because its easier to manage.
A simplistic example of how to get the folders in the current directory would look something like this
import System.IO ;
import System.Collections.Generic ;
private var _curPath :String ;
private var _curDirectoryFolderPaths : List.<String> = new List.<String>() ;
function GetCurDirFolders () {
_curPath = Directory.GetCurrentDirectory();
for ( folderPath in Directory.GetDirectories( _curPath )) {
try {
_curDirectoryFolderPaths.Add( folderPath );
}catch ( error ) {
Debug.Log( error);
}
}
Debug.Log("Found "+_curDirectoryFolderPaths.Count.ToString() + " Folder(s) in this Directory " );
}
Do the same for Directory.GetFiles() which will return the paths of the files in that Directory, you can filter files if you need and search only specific file types
All this can be done in UNITY FREE.
Once you got the path for lets say a mp3 audio track for example you can load using Unity's WWW Class.To access directories and files this way you need to put this on top of you script import System.IO ;
To Use Generic Lists put this at the top of your Script import System.Collections.Generic ;
To create a list do this
var _curDirFilePaths : List.<String> = new List.<String>();
Hope this Helps but if you're stuck let me know.
Here's a rough prototype of my DIY File Browser for an Android App im creating.
Thanks to Woozybites for your answer and detailed code eventhough I'm a newbie your code sample looks like Javascript and I'm learning C# so I will study the Unity Docs for the Directory Class in C#.
Hi Colin2015 Im Happy to help.
The Directory class is not in the Unity Docs it's .NET and you can find the documentation in the $$anonymous$$SDN WebSite, there will be C# examples as well as JS.
Just follow the links on my answer ,change the .Net framework to 2.0 on the msdn site and look for the C# examples.
Hope it Helps, but if you get stuck , give me a buzz :)
This code can browse only current directive. Is there is any option to find .mp3 on device(mobile)?
Answer by captaincrunch80 · Apr 12, 2012 at 12:26 AM
AFAIK for this kind of stuff you need the PRO version of unity. A custom Manifest file will be neccessary (you need full Memory access on the phone) and a java native plugin for unity to handle the file loading and stuff via Android Framework functions.
If you have Pro this is the way to go.
Answer by captaincrunch80 · Apr 12, 2012 at 10:36 PM
Hi again,
ok I have taken a deeper look, it can be managed by Intend instead of scanning the USB storage yourself.
You can handle it with Intend ACTION GET CONTENT. Take a look here http://stackoverflow.com/questions/9577576/how-to-call-in-built-android-music-menu-in-my-development (The second answer looks good)
Check out this too http://unity3d.com/support/documentation/Manual/Android-Launch%20an%20Android%20Application%20from%20a%20Unity%20Application.html and set up your unity project in Eclipse.
Maybe you can do this without pro and I was wrong (I thought native java integration requires Pro, but if the AndroidJavaObject works it should be possible.). Give it a try!
Best of luck! Maybe you can report back if it works. Would be nice to know.