Can I replace an audio clip with a user selected file on android?

I wrote a music visualizer using songs just dragged into my unity project to test it. Now, I want to replace that with a menu for users to select songs from their library. So far I am using javascript. Please point me in the right direction.

Alright. To load an external music file and put in in an audioSource at runtime (with javascript), you need a combination of “System.IO” and “WWW”.

Here is a stripped down version of how I solved it for my project. I have not tested this exact code, but it should work if you put an audiosource and this script on a game object.

This should pick the first song alphabetically in the music folder on an android device and play it. There is no error handling so an absent music folder will cause issues like no song being played.

var MusicFolder : System.IO.DirectoryInfo;
var myClip : WWW;
var myPath : String;

function Start()
{
	myPath = "/mnt/sdcard/music";
	MusicFolder = new System.IO.DirectoryInfo(myPath);
	myClip = new WWW("file:///" + MusicFolder.GetFiles()[0].FullName);
	audio.clip = myClip.GetAudioClip(false, false);
}
function Update()
{
	if (!audio.isPlaying && audio.clip.isReadyToPlay){
		soundPlayer.audio.Play();
	}
}

I think it’s Prime31 that has an Android library for accessing pictures from the phone’s library. Maybe it could be used as a template for accessing music too.

Here is C# version(now only PC version):

    private System.IO.DirectoryInfo MusicFolder;
 	private WWW myClip;
 	public string myPath;
	private AudioSource audioSource;
	
	void Start () {
		audioSource = GetComponent<AudioSource>();
		MusicFolder = new System.IO.DirectoryInfo(myPath);
		myClip = new WWW("file:///" + MusicFolder.GetFiles()[0].FullName);
		audioSource.clip = myClip.GetAudioClip(false, false);
	}
	
	
	void Update () {
		if (!audioSource.isPlaying && audioSource.clip.isReadyToPlay){
         	audioSource.Play();
     	}
	}

In future planing to make it useful for mobile devices