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 Storm024 · Jun 09, 2014 at 11:33 AM · fileplaymusicfolderstream

Streaming music from a folder or file

Currently I am developing a racing game, the gimmick of this game is the players ability to customize practically everything, from your car to your music. Hence, as you have probably figured out, the player needs to be able to play their own music. Currently I am using the following code, however when I debug, have the music all mapped and all, it simply doesn't play.

Code for setting the music using UnityEngine; using System.Collections; using System.IO;

 public class OptionsMenu : MonoBehaviour {
 
     public bool MenuState = false;
     public MainMenu main;
     public GUISkin DatGUI;
     string MLocation = string.Empty;
     void OnGUI()
     {   if (MenuState)
         {
             GUI.skin = DatGUI;
             //Allows the player to enter the music list location
             GUI.Box(new Rect(0, 40, Screen.width, 60),
                 "This is the music list location. Note that forward slashes should be used, not backslashes. Note that all music should be in OGG or WAV format. Format factory is recommended if conversion needed.");
             MLocation = GUI.TextField(new Rect(0,100, Screen.width, 40),MLocation);
         // after the player clicks the following button, the game does a whole bunch of complex stuff. Essentially it makes a list of all the music available before setting the length of this list.
         // NOTE TO SELF!!! This is without the file:/// appended to the start. When importing through WWW that file:/// needs to be added to the start
         if (GUI.Button(new Rect(0,140, Screen.width, 40), "Set directory"))
         {
             PlayerPrefs.SetString("MusicLocation",MLocation);
             DirectoryInfo info = new DirectoryInfo(MLocation);
             FileInfo[] fileinfo = info.GetFiles();
             // The above code: sets the files music location, then gets a list of files within the location 
             int FileListLength = 0;
             foreach (FileInfo file in fileinfo)
             {
                 if (file.Extension == ".ogg" || file.Extension == ".wav")
                 {
                     PlayerPrefs.SetString(FileListLength.ToString(), file.Name);
                     FileListLength++;
                 }
             }
             PlayerPrefs.SetInt("MusicList", FileListLength);
             //this goes through each file, validating that it can be used.
         }
         GUI.Box(new Rect(0, 180, Screen.width, 40), "Playlist location: " + PlayerPrefs.GetString("MusicLocation") + " Playlist size: " + PlayerPrefs.GetInt("MusicList") + "Example Track: " + PlayerPrefs.GetString("1"));
         if (GUI.Button(new Rect(0, 0, Screen.width, 40), "BACK"))
             {
                 MenuState = false;
                 main.MenuState = true;
             }
         }
         }
 }

Code for playing the music private int ChosenTrack; // Once per frame update is called. This tells the game to take user input and tells the AI to respond to the world around it. void Update() {

         UserInput();
         foreach (Car CurrentCar in CarList)
         {
             AIInput.AIinput(CurrentCar);
         }
     }
 // this sets the players music so that it starts playing
     void Start()
     {
         ChosenTrack = (int)(UnityEngine.Random.value * PlayerPrefs.GetInt("MusicList"));
         string str = PlayerPrefs.GetString(ChosenTrack.ToString());
         WWW www = new WWW("file:///" + PlayerPrefs.GetString("MusicLocation") + PlayerPrefs.GetString(ChosenTrack.ToString()));
         audio.clip = www.GetAudioClip(false,false);
         
     }
Comment
Add comment · Show 1
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 PouletFrit · Jun 09, 2014 at 12:44 PM 0
Share

you might need to yield on www. It probly wont return the file instantly, even though its local

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

21 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

Related Questions

Can I change the folder that my lightmap .exr files are placed? 1 Answer

Is there any way to make the headphone pause/unpause controls work when the Unity app is running, to pause/unpause background app(apple music/google music) music? Android app. 0 Answers

File.Create not working 0 Answers

How do I find a folder I can write data to on Android? 3 Answers

Music play when MOVING.. 2 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