• 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
Question by fschaar · Sep 08, 2014 at 02:09 PM · wwwaudioclip www load

Load ogg file into audioclip

Hi, I created a little audio editor i need as tool for a game, were other people are able to edit custom audio files. So I can't load them through resources.load. I at least came up wir using the www class. but the problem is, that I need to use Coroutines to make it work - i can't get around it. Here is my code so far:

     public static AudioClip GetAudioClip(string mp3FileName) {
         string s = MP3_FILENAME_UMLAUT_CODE_CHAR.ToString();
         mp3FileName = mp3FileName.Replace("ö", "o" + s).Replace("Ö", "O" + s).Replace("ü", "u" + s).Replace("Ü", "U" + s).Replace("ä", "a" + s).Replace("Ä", "A" + s).Replace("ß", "s" + s).Replace(" ", "_");
         mp3FileName = MP3_RESOURCES_SUB_DIR + mp3FileName;
 
         AudioClip mp3Clip;
 
         yield return StartCoroutine(loadAudioFile("File:/" + mp3FileName + MP3_FILE_EXTENSION, value => mp3Clip = value));
     }
     IEnumerator loadAudioFile(string url, System.Action<AudioClip> result)
     {
         WWW www = new WWW(url);
         float elapsedTime = 0.0f;
         
         while (!www.isDone)
         {
             elapsedTime += Time.deltaTime;
             if (elapsedTime >= 10.0f) break;
             yield return null;
         }
         
         if (!www.isDone || !string.IsNullOrEmpty(www.error))
         {
             Debug.LogError("Load Failed");
             result(null);    // Pass null result.
             yield break;
         }
         
         result(www.audioClip); // Pass retrieved result.
     }

Problem is obviously, that GetAudiClip must be of type IEnumerator, but then I can't return the value to the calling function. Is there any way around this?

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by AAAAAAAAAE · Jul 07, 2019 at 07:28 AM

 private IEnumerator LoadAudio(string audioName, int track_id, float start_time)
     {
         
 
         using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(
                                         musicPath.Replace("#","%23") + 
                                         
                                         audioName, AudioType.OGGVORBIS))
         {
             
             var synRes = www.SendWebRequest();
             yield return synRes;
 
             if (www.isNetworkError)
             {
                 Debug.Log(www.error);
             }
             else
             {
                 stream_music = DownloadHandlerAudioClip.GetContent(www);
                 musicSource.clip = stream_music;
                 musicSource.time = start_time;
                 current_track_num = track_id;
                 current_track = GetCurrentTrack();
                 musicSource.Play();
             }
         }
 
     }

Comment

People who like this

0 Show 0 · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW.GetAudioClip Freeze 1 Answer

WWW fails to detect ogg audio format when parameters present in url 1 Answer

How to play local audio clips in webgl 0 Answers

How to use external audioclips from the Android Music folder? 1 Answer

loading/streaming audioclip using www class [android] 0 Answers


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