- Home /
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.
Answer by TheJordan · May 09, 2013 at 09:49 AM
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();
}
}
Hi, I have tested out this solution and the path does not work. I have tried using a path for my PC and it works perfectly. The android path it is translating it to C:\mnt\sdcard\music. Did you use a different path or add anything to that code to make it work? Would appreciate an answer. Thanks a lot
I have also tested this solution right now and it works, maybe you forgot to give READ_EXTERNAL_STORAGE-permission. The code snippet works except there is no audio nor soundPlayer declared.
What data types do the soundPlayer and audio variables need to be declared as?
"/mnt/sdcard/music"; is convered to c:\mnt\sdcard\music and hence i cannot load files from android...works perfectly on pc any solution please??
Answer by DaveA · May 02, 2013 at 11:06 PM
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.
Thanks. I'd rather program it myself than pay someone else for a plugin though. I'll post the answer here when I figure it out for all you people from Google in the future.
Hey, I know it past few years since you posted this, just to ask did you found way to select music from mobile that plays in unity audiosource?
Answer by QuDi · Jul 31, 2018 at 09:22 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613101429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
loading/streaming audioclip using www class [android] 0 Answers
Sound not playing on Galaxy S4 device 0 Answers
Where to store files in Android that can be accessed later via path? 1 Answer
WWW.GetAudioClip Freeze 1 Answer
Android sound distorted 2 Answers