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 /
avatar image
0
Question by LeytonViner · Oct 10, 2018 at 05:50 PM · audioaudioclipfilesdirectory

How to get a list of AudioClips from a directory at runtime?

ok, so I'm making a music player and i need to get all mp3 files from a path in the computer, then store those files as a list of AudioClips that the AudioSource can play. The problem is, I have no clue on how to do this. Can anybody help me with this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by W01Fi3 · Oct 10, 2018 at 08:44 PM

@LeytonViner Well, after a lot of research and testing, I found a way to do this. Of course there's always a better way to do this but this is the best I can come up with

 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using UnityEngine;
 
 //Don't forget to use System.IO
 public class GetAudioFiles : MonoBehaviour
 {
     //Directory of folder to be searched anywhere on the computer
     public string FileDirectory;
 
     //Audio source
     public AudioSource Source;
     //Current sound playing
     public AudioClip Clip;
 
     //List of all valid directories
     List<string> Files = new List<string>();
     //List of all AudioClips
     List<AudioClip> Clips = new List<AudioClip>();
 
     private void Start()
     {
         Source = GetComponent<AudioSource>();
 
         //Grabs all files from FileDirectory
         string[] files;
         files = Directory.GetFiles(FileDirectory);
 
         //Checks all files and stores all WAV files into the Files list.
         for (int i = 0; i < files.Length; i++)
         {
             if (files[i].EndsWith(".wav"))
             {
                 Files.Add(files[i]);
                 Clips.Add(new WWW(files[i]).GetAudioClip(false, true, AudioType.WAV));
             }
         }
         //Calls the below method
         PlaySong(0);
     }
     public void PlaySong(int _listIndex)
     {
         Clip = Clips[_listIndex];
         Source.clip = Clip;
         Source.Play();
     }
 }

Remember to make sure all files are WAV files, and to assign the FileDirectory variable, or even make an array of directories

Comment
Add comment · Show 8 · 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 LeytonViner · Oct 18, 2018 at 04:53 PM 0
Share

Did you test this? for some reason theres an error about fmod when trying to add the song to the clips

avatar image W01Fi3 LeytonViner · Oct 18, 2018 at 05:07 PM 1
Share

Ok, I just tested it and everything works fine.

All you need to do is assign the variable FileDirectory to a valid local file path on your computer As well as that, ensure that all audio files at that path are .wav files, else it will not work. Obviously, you need an audio source on the object the script is attached to, or assigned Source to any audio source within the scene

avatar image LeytonViner W01Fi3 · Oct 19, 2018 at 01:15 PM 0
Share

what version are you using? it doesnt work in 2017.3

Show more comments

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

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

How to play several audio cllips one after another. 3 Answers

Slowly fade audio's pitch on key press? 3 Answers

Problem with playing sound on Trigger-Enter 2 Answers

How to get decibel's value of game's sound? 0 Answers

Game lags when loading AudioClip from Resources 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