Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zero3growlithe · Dec 06, 2013 at 11:23 AM · coroutineloadinglagaudioclipbackground

Download an audio file in background without lags.

Hello, I have a problem with loading an audio file from HDD, more precisely while it loads, the game stops until it is fully downloaded even if I use yield's or other coroutine methods. Am I doing something wrong or it has to be done using streaming? (which I once used but want to avoid it this time, too much work and code involved)

Here's the code am using right now:

 @HideInInspector var musicFolderContents : String[];
 @HideInInspector var customPlaylistSwitch : int[];
 private var musicPlaylist : int[];
 private var actualMusicID : int;
 private var externalMusicScan : int;
 private var bufferedAudioclip : AudioClip;
 private var HDDaudioFile : WWW;
 var playNextTrack : boolean;
 var actualAudioTrack : String;
 function MusicPlayer (){
     if (playNextTrack){
         bufferedAudioclip = null;
         // Play music from internal library
         if (musicPlaylist[actualMusicID] < originalMusic.length){
             bufferedAudioclip = originalMusic[musicPlaylist[actualMusicID]];
         } else{ 
         // Play music from external folder
             actualAudioTrack = Path.GetFileNameWithoutExtension("file://"+musicFolderContents[musicPlaylist[actualMusicID]-originalMusic.length]);
             if (Application.isEditor) HDDaudioFile = new WWW ("file://"+musicFolderContents[musicPlaylist[actualMusicID]-originalMusic.length]);
                 else HDDaudioFile = new WWW ("file://"+Application.dataPath+"/Music/"+musicFolderContents[musicPlaylist[actualMusicID]-originalMusic.length]);
             bufferedAudioclip = HDDaudioFile.GetAudioClip(false, false);
         }
         actualMusicID = (actualMusicID + 1) % musicPlaylist.length;
         playNextTrack = false;
     }
     if (bufferedAudioclip != null){
         if (musicPlaylist[actualMusicID] < originalMusic.length){
             audio.clip = bufferedAudioclip;
             audio.Play();
             bufferedAudioclip = null;
         } else{
             if (HDDaudioFile.isDone){
                 if (bufferedAudioclip.isReadyToPlay){
                     audio.clip = bufferedAudioclip;
                     audio.Play();
                     bufferedAudioclip = null;
                 }
             }
         }
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArkaneX · Dec 06, 2013 at 12:12 PM 0
Share

Are you sure it's downloading that blocks your app? I suggest adding print("before") before yield, print("after") after yield, and print("update") inside your Update function. There should be at least a few "update" texts logged to the console between "before" and "after". That means your program is not blocked.

Btw - there's no need to check www.isDone - if coroutine resumed, then isDone should be true.

avatar image zero3growlithe · Dec 06, 2013 at 03:14 PM 0
Share

Well, tried to check where the lag occurs and... no idea! I can't find place where prints would appear like: one before lag and one after, they always appear together... I altered the code in my question so can u try to point out where the loading might make the game stop for a moment?

avatar image zero3growlithe · Dec 06, 2013 at 03:33 PM 0
Share

Ok sooo I discovered that loading the file and assigning it to AudioSource does not create the stop, what does create it is the audio.Play(); in this case... sooooooooo, what can be done about this? EDIT: Correction, audio.clip.isReadyToPlay cause it... dafuq, $$anonymous$$dblow and whatever

avatar image ArkaneX · Dec 06, 2013 at 03:38 PM 1
Share

Hmm - I've just read a forum topic related to such freezes, and it looks like this might be related to file decompression. You might check the thread for more information.

avatar image zero3growlithe · Dec 06, 2013 at 04:11 PM 0
Share

hmmmm, from all those posts I found in there and links they posted it seems I need to use "strea$$anonymous$$g" method afterall... oh well, thanks for helping me figure out what causes the problem :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load a DLL only when needed ? 1 Answer

How to select a random audioclip to be played in multiple places 1 Answer

Loading screen with LoadLevelAsync 0 Answers

Loading a level and some resources asynchronously 0 Answers

Fade, Then Stop all other audiosources attached to gameobject? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges