Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by phildragon · Oct 23, 2015 at 08:14 PM · script.

c# Script How to create an AudioClip from the file's path location ?

Let's says i have a wav file in this current location "C:\AudioTest" and from Unity from c# script, i want to create an AudioClip instance from this audio file. is:

I tried this: audioclip = (AudioClip)Resources.Load(path_file_name, typeof(AudioClip));

Nothing happens.

From Unity hierarchy i have an AudioSource component in my GameObject and no matter if i put an audio file or not in the AudioSource component, nothing happens.

Can someone help me ?

Thanks you for your attention.

Greetings.

Phil

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Statement · Oct 24, 2015 at 01:39 AM

Resources.Load will only work for assets stored in a Resources folder (see loading resources at runtime).

Use WWW.audioClip.

(Yes, it's a funny name for a class that can load audio clips from disk)

Prefix path with "file:///" as the reference says.

 string path = "C:/AudioFiles/audio1.wav";
 string url = "file:///" + path;
Comment
Add comment · Show 6 · 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
avatar image phildragon · Oct 24, 2015 at 03:29 AM 0
Share

Thank you so much for taking your time ti answer me. I fallow this example from the link http://docs.unity3d.com/ScriptReference/WWW-audioClip.html

And unfortunately nothing happens. At least there is no error messages.

I don't know if i must use Application.dataPath , Application.persistentDataPath or simply a folder from my c: drive "c:/AudioFiles/audio1.wav"

If you can try to play an audio file in your computer from an empty Unity project and try the scenario that makes you help me better.

Here is my script:

using UnityEngine; using System.Collections;

public class StringAudioConverter : $$anonymous$$onoBehaviour { public string url = Application.dataPath + "/Resources/audio1.wav"; //Application.persistentDataPath; //"C:/simFIL/Audio/audio2015-10-23 14h59m04s.wav"; public AudioSource source; private void Start() { WWW www = new WWW(url); source = GetComponent(); source.clip = www.audioClip; } private void Update() { if (!source.isPlaying && source.clip.isReadyToPlay) source.Play(); } }

===

Thank and have a nice week end.

avatar image phildragon · Oct 24, 2015 at 03:30 AM 0
Share

Thank you so much for taking your time ti answer me. I fallow this example from the link http://docs.unity3d.com/ScriptReference/WWW-audioClip.html

And unfortunately nothing happens. At least there is no error messages.

I don't know if i must use Application.dataPath , Application.persistentDataPath or simply a folder from my c: drive "c:/AudioFiles/audio1.wav"

If you can try to play an audio file in your computer from an empty Unity project and try the scenario that makes you help me better.

Thanks.

Phil

avatar image Statement phildragon · Oct 24, 2015 at 09:57 AM 0
Share

Prefix path with "file:///" as the reference says.

"file:///C:/AudioFiles/audio1.wav"

avatar image phildragon Statement · Oct 25, 2015 at 08:01 PM 0
Share

It's working. Thank you so much! :)

avatar image Tuitaio · Apr 10, 2018 at 09:09 PM 0
Share

Does this still work? I'm able to load an AudioClip, but its empty, the name is "".

avatar image Tuitaio Tuitaio · Apr 10, 2018 at 09:43 PM 0
Share

Got it working, had to add this:

 WWW a = new WWW(url);
             
 while (a.progress < 0.1)
 {
     yield return new WaitForSeconds(0.1f);
 }



avatar image
1

Answer by phildragon · Oct 25, 2015 at 09:11 PM

For people who got this issue or similar issue in the future, i want to let you know that the tutorial from the link http://docs.unity3d.com/ScriptReference/WWW-audioClip.html is a good reference but it has some limitation.

Indeed the contructor WWW( string url_path) can only take constant string value or a string variable which has only one value (like this "file:///c:/Audio/test.wav"). It will not work if you do something like this: string link = ""; string path = "file:///c:/Audio/"; string filename = "test.wav" link = path + filename;

WWW www = new WWW( link ); - nothing will happens without any error message if you compile your program. It will work only if you use this: string link = "file:///c:/Audio/test.wav"; WWW www = new WWW( link );

or this WWW www = new WWW( "file:///c:/Audio/test.wav" );

If you want to proceed with editable variable, use this tutorial from the link references: http://answers.unity3d.com/questions/652919/music-player-get-songs-from-directory.html

that concerns a list of audioclip with a UI (OnGUI) in it but it is very helpfull. Thanks.

Phil

Comment
Add comment · Show 4 · 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
avatar image Statement · Oct 25, 2015 at 09:29 PM 1
Share

I appreciate your dedication on trying to warn people, but....

 string link = "";
 string path = "file:///c:/Audio/";
 string filename = "test.wav"; 
 link = path + filename;

...and ...

 string link = "file:///c:/Audio/test.wav";

Produce the exact same string and doesn't affect the operation of WWW. Perhaps you had an error when you were testing it out or you were getting paths from another API which return backslashes "\" ins$$anonymous$$d of "/", or you made an error concatenating the strings.

avatar image phildragon Statement · Oct 25, 2015 at 09:46 PM 0
Share

I agree that it's the same thing ... and i believe that it's a bug from Unity. But i test it many times and i got that unpredictable issue. I didn't make any error regarding the "\" and "/". If it's not a bug from Unity, it's an issue with my machine because i didn't test it to another PC.

avatar image Statement phildragon · Oct 25, 2015 at 09:59 PM 1
Share

If that was a bug, it would mean that $$anonymous$$ono would be broken at its very core and nothing would work.

avatar image phildragon Statement · Oct 25, 2015 at 11:12 PM 0
Share

Thanks a lot Statement. The info that you provide was very helpful. Yeah sometimes machines are like humans: they work only when they want to.

avatar image
0

Answer by slally17 · Dec 02, 2020 at 12:33 AM

WWW is now obsolete and UnityWebRequest has to be used instead.

Comment
Add comment · Show 1 · 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
avatar image cyberjj999 · Dec 26, 2020 at 04:12 AM 1
Share

@slally17 could you provide the updated answer? ``` UnityWebRequest www = UnityWebRequest.Get(path);```

I have this but im unsure how to play the clip

avatar image
0

Answer by manro2 · Feb 10, 2021 at 01:03 PM

https://github.com/naudio/NLayer if u need simpe way!

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

35 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 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

Have object fly towards camera 0 Answers

Animation only plays in one of the objects 1 Answer

Load scene after battle 0 Answers

Action (by collider) 0 Answers

Use c# "ref" with a GameObject or Component 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