- Home /
The question is answered, right answer was accepted
[closed]Music works in editor but not in build?
Hiya. I've been working on a simple audio player for learning purposes and I've hit a snag. Everything works PERFECTLY....until I build it. In the editor its flawless but when I build it, essentially nothing happens. everything, gui, song list, ect, loads fine. but the music wont start. upon further investigation, I found that "audio.isReadyToPlay" was returning false in the build version but true in the editor. Im completely stumped as to why as everything I've looked at says it should be working just fine! Heres the main script. There are two to this package but theres no need for the other one as its simply a controller and does not effect the actual playing of music.
import System.IO;
import System.Collections.Generic;
var temp = 0.0;
var timeout = 0.0;
//var playy = true;
var wwwar :List.<AudioClip>;
var fileinfo : Array;
var fil: int;
var cursong: int;
var ffwd : GUIStyle;
var rwd: GUIStyle;
var refresh:GUIStyle;
var stop:GUIStyle;
var pause : GUIStyle;
var play : GUIStyle;
var original: List.<String>;
var ischanged: boolean;
var originalWidth = 1280.0; // define here the original resolution
var originalHeight = 720.0; // you used to create the GUI contents
private var scale: Vector3;
var ScrollVector : Vector2 = Vector2.zero;
PlayerPrefs.DeleteAll();
function Start() {
//var dataloc : String = Application.dataPath.Substring(0,Application.dataPath.length - " Asset".length);
//var www = new WWW("File://Fearofdark - Motorway - 02 Suburban Supernatural.ogg");
//print(Application.dataPath.Substring(0,Application.dataPath.length - " Asset".length));
//wwwarray.clear();
try{
fileinfo = DirectoryInfo(Application.dataPath + "//..//music//").GetFiles("*.ogg");
print("success");
}catch(e:Exception){
print(e);
print("failure");
Directory.CreateDirectory((Application.dataPath + "//..//music//"));
fileinfo = DirectoryInfo(Application.dataPath + "//..//music//").GetFiles("*.ogg");
}
for(i = 0 ; i < fileinfo.length ; i++){
var tempwww = WWW("File://"+fileinfo[i]);
wwwar.Add(tempwww.audioClip);
print("added file: " + tempwww);
}
audio.clip = wwwar[0];
print("added song to audio.clip");
cursong = 0;
fil = fileinfo.length;
}
function LoadMusicList(){
wwwar.Clear();
temp = 0.0;
audio.timeSamples = temp;
Start();
musicload(0);
}
function OnGUI () {
scale.x = Screen.width/originalWidth; // calculate hor scale
scale.y = Screen.height/originalHeight; // calculate vert scale
scale.z = 1;
var svMat = GUI.matrix; // save current matrix
// substitute matrix - only scale is altered from standard
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
// draw your GUI controls here:
if (!audio.isPlaying){
if(GUI.Button(Rect(800,50,50,50),"",play)){
PMusic();
}
}else{
if(GUI.Button(Rect(800,50,50,50),"",pause)){
PMusic();
}
}
temp = audio.timeSamples;
try{
temp = (GUI.HorizontalSlider(Rect(45,78+(cursong*60),250,20),temp,0,audio.clip.samples));
}catch(e:Exception){
print(e);
temp = (GUI.HorizontalSlider(Rect(45,78+(cursong*60),250,20),temp,0,1));
}
audio.timeSamples = temp;
GUI.Label(Rect(45,62+(cursong*60),100,100), stt(audio.time));
if (GUI.Button(Rect(745,50,50,50),"",rwd)){ playprev();}
if (GUI.Button(Rect(855,50,50,50),"",ffwd)){ playnext();}
if (GUI.Button(Rect(910,50,50,50),"",refresh)){ LoadMusicList();}
if (GUI.Button(Rect(965,50,50,50),"",stop)){ stopsong();;}
//print(DirectoryInfo(Application.dataPath + "//..//music//"));
// end your GUI controls here:
GUI.matrix = svMat; //restore matrix
}
function musicload(t){
timeout = 0.0;
try{audio.clip = wwwar[t];}catch(e:Exception){LoadMusicList();audio.clip = wwwar[t];print(e);}
while(!audio.isPlaying && timeout < 10){
yield WaitForSeconds(0.5);
timeout = timeout + 0.5;
PMusic();
}
timeout = 10;
temp = 0;
audio.timeSamples = temp;
cursong = t;
}
function PMusic(){
if(!audio.isPlaying && audio.clip.isReadyToPlay){
print("test");
audio.Play();
}else{
print("test2");
print(audio.isPlaying);
print(audio.clip.isReadyToPlay);
audio.Pause();
}
}
function stt(sec){
if (float.IsNaN(sec) || sec == Mathf.Infinity){sec = 0.0;}
var hours = Math.Floor(sec/3600).ToString("00");
var minutes = Math.Floor(sec / 60).ToString("00");
var seconds = (sec % 60).ToString("00");
return (hours + ":" + minutes + ":" + seconds);
}
function playnext(){
if(cursong == fil-1){
musicload(0);
}else{
musicload(cursong+1);
}
}
function playprev(){
if(cursong == 0){
musicload(fil-1);
}else{
musicload(cursong-1);
}
}
function stopsong(){
audio.Stop();
temp = 0.0;
audio.timeSamples = temp;
}
I know its sloppy, I don't care. I know I didn't do everything in the most perfect of ways, again, I don't care. I'm still new to this whole thing so I hope you understand. Also, I can't use resources.load as I need to be able to import them raw on load. TYVM for any help offered!
Answer by ackley14 · Apr 03, 2015 at 03:30 PM
Turns out unity uses slightly different wording between the editor file system, and the build filesystem :
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Prefabs are instantiated in editor but not in executable 2 Answers
How to fix: !CreateDirectoryRecursive(fullpath) 6 Answers
Global Illumination is radically different between editor and built apps 0 Answers
Is there an event triggered before editor starts rebuilding scripts? 1 Answer