Unity – WWW – Get Audio from Web
1. MAIN TOP MENU> File> New Project…
2. MAIN TOP MENU> GameObject> Create Empty, name it ‘Music Player’
3. Hierarchy> select ‘Music Player’> Inspector> ‘Add component’> Audio Source check ‘Play On Awake’, check ‘Loop’
4. Hierarchy> select ‘Music Player’> Inspector> ‘Add component’> New Script> JavaScript> name it LoadMusic.js
5. LoadMusic.js
#pragma strict function Start () { // Get Audio from Web var www = new WWW('http://www.lucedigitale.com/blog/wp-content/gimme-code/css/images-gimme-code-2013-0016/bgmusic.ogg'); audio.clip = www.audioClip; } function Update () { if(!audio.isPlaying && audio.clip.isReadyToPlay) audio.Play(); }
6. Build and Play the build to hear the audio.
Sometimes you need wait for a few minutes if the web connection will be slow.
WARNING: If you use TOP Play button to preview the game you will get the console message:
‘Exception: Expected root element to be
It is a security layer applied to the Web Player, you have to Build and Play the final HTML file to hear audio.
—
Reference:
http://docs.unity3d.com/Documentation/ScriptReference/WWW.html