Unity plays audio file reference code:
1 Public voidPlay (stringStrsoundname,floatAutodestroytime = 0f,BOOLBloop =false,floatFpertime =1f)2 {3 if(!strsoundname.equals (""))4 {5 //Set background music6AudioClip clip = resources.load<audioclip>(strsoundname);7 8 if(_csaudio = =NULL)9 awake ();Ten if(Clip! =NULL) One { A_csaudio.loop =false;//with true here there will be bugs, sometimes not loops -_csaudio.clip =clip; - the if(_fdelay >0.0f) - _csaudio.playdelayed (_fdelay); - Else - _csaudio.play (); + - +_bloop =bloop; A_fpertime =Fpertime; at - //Loops - if(_bloop) - { -Invoke ("Autoloop", _fpertime); - } in Else - { to //non-cyclic automatic destruction + floatFautodestroytime =clip.length; - if(Autodestroytime >0) Fautodestroytime =Autodestroytime; theInvoke ("Stop", autodestroytime); * } $ }Panax Notoginseng Else - { the //sound load failure, direct destruction + Stop (); A } the + - } $}
Unity plays audio files