- Home /
Overwrite an Object's AudioSource's AudioClip?
EDIT
So I sort of fixed it.
It was as simple as
WWW www = new WWW(path + "music.wav");
audio.clip = www.audioClip;
But...notice the .wav. Frankly, this is hugely disappointing, and very embarassing on Unity's end to see that they don't support the www loading of MP3 FILES. No excuses, Unity. Try harder. Guess my project is going to probably require a file conversion on the end-users end because this very, very simple functionality doesn't work for Windows executables. Sad, really.
Hi there! So I'm trying to add the ability for the player to use their own music file.
I want my script to look for a specific folder, and if there's a file in there, load it by my specific name. This should then be an audio.clip that overwrites the object's default music audio.clip.
the music is to be grabbed from the build's Resource folder, inside of a folder called "Music" that I will be adding to my distributed build.
Here's what I have:
using UnityEngine;
using System.Collections;
//
using System.IO; //in order to create folder
//
public class customMusic : MonoBehaviour {
// Use this for initialization
void Start () {
//Make string of file path
string path = "file://"+(Application.dataPath)+"/Resources/Music";
//if a file exists in the Music Folder...
if (Directory.GetFiles(path).Length > 0)
{
//then set the audio clip to be their music
audio.clip = (AudioClip)Resources.Load(path +"music.mp3");
//audio.clip = Resources.Load(path + "music.mp3", typeof(AudioClip));
};
}
//End Start
// Update is called once per frame
void Update ()
{
}
//end Update
}
I just tried it out. No errors, but no effect. I have a music file of mine renamed "music.mp3" and put into that build folder, but nothing happens. Am I incorrectly defining audio.clip? Should I be more specific about the audio source of my musicBox object that holds this script? Any help would be appreciated!
Now, this is separate from the Unityscript that I have on the same object that handles audio playing and pausing, so that's no issue. It works. But I just tested my script and it played the same default song that I have set in the inspector for the musicBox object, and not the (data path)Resources/Music/music.mp3 file.
I checked my output log, and I get this error:
NullReferenceException: Object reference not set to an instance of an object at customMusic.Start () [0x00000] in :0
path +"music.mp3"
/Resources/$$anonymous$$usicmusic.mp3
Oh my god...that's what I did there? I forgot a single slash. Let me take a look at this!
Edit: Nope :( I changed "path" to "file://"+(Application.dataPath)+"/Resources/$$anonymous$$usic/" - I added the slash - and it still loads the default audioclip.
Your answer
Follow this Question
Related Questions
Multiple AudioSources overwriting a looping AudioSource 1 Answer
Working Sound Clip not Audible on iOS Devices 0 Answers
How to get C# Array to Load into Audio Source 0 Answers
GetSpectrumData - what does it fill the samples argument with exactly? 0 Answers
AudioClip stuttering while loop is OFF and not using function Update? 1 Answer