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 /
This question was closed Mar 15, 2015 at 03:22 PM by KnightRiderGuy for the following reason:

Answers are non helpful and only produce hints and not answers, Question is not specific enough.

avatar image
0
Question by KnightRiderGuy · Mar 11, 2015 at 06:22 PM · c#listmusicarraylist

Add a Play Next Song And A shuffle Option To Music Player C#

I have this music player script that I would really like to be able to add a couple more options too: 1) a button for shuffle songs. 2) Play next song automatically. this is an awesome music player only it's currently lacking the ability to play the next song in the array.

here is the music player Script. With help from some other postings it now currently has the ability to display using UI text the current song playing. So it's getting there. :)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using UnityEngine.UI;
 
 public class AltMusicPlayer : MonoBehaviour
 {
 
     public Text MusicListText;// Reference to my UI Display Text for Songs
     private string display = "";//NEW Delete if does not work!!
     public enum SeekDirection { Forward, Backward }
     
     public AudioSource source;
     public List<AudioClip> clips = new List<AudioClip>();
     
     [SerializeField] [HideInInspector] private int currentIndex = 0;
     
     private FileInfo[] soundFiles;
     private List<string> validExtensions = new List<string> { ".ogg", ".wav" }; // Don't forget the "." i.e. "ogg" won't work - cause Path.GetExtension(filePath) will return .ext, not just ext.
     private string absolutePath = "./Audio/Music"; // relative path to where the app is running - change this to "./music" in your case
 
     void Start()
     {
         //being able to test in unity
         if (Application.isEditor) absolutePath = "Assets/Audio/Music";
         
         if (source == null) source = gameObject.AddComponent<AudioSource>();
         ReloadSounds();
     }
 
     //My New UI Buttons Added
     public void PlayPrevious (){
         Seek(SeekDirection.Backward);
         PlayCurrent();
     }
 
     public void PlayCurrentClip () {
         PlayCurrent();
     }
 
     public void PlayNext () {
         Seek(SeekDirection.Forward);
         PlayCurrent();
     }
 
     public void ReloadAudio () {
         ReloadSounds();
     }
 
 
     //End My New UI Buttons Added
     
     void Seek(SeekDirection d)
     {
         if (d == SeekDirection.Forward)
             currentIndex = (currentIndex + 1) % clips.Count;
         else {
             currentIndex--;
             if (currentIndex < 0) currentIndex = clips.Count - 1;
         }
     }
     
     void PlayCurrent()
     {
         source.clip = clips[currentIndex];
         source.Play();
         Debug.Log (source.clip);
         MusicListText.text = clips[currentIndex].name;
     }
     
     void ReloadSounds()
     {
         clips.Clear();
         
         // get all valid files
         var info = new DirectoryInfo(absolutePath);
         soundFiles = info.GetFiles()
             .Where(f => IsValidFileType(f.Name))
                 .ToArray();
         
         // and load them
         foreach (var s in soundFiles)
             StartCoroutine(LoadFile(s.FullName));
     }
     
     bool IsValidFileType(string fileName)
     {
         return validExtensions.Contains(Path.GetExtension(fileName));
         // Alternatively, you could go fileName.SubString(fileName.LastIndexOf('.') + 1); that way you don't need the '.' when you add your extensions
     }
     
     IEnumerator LoadFile(string path)
     {
         WWW www = new WWW("file://" + path);
         print("loading " + path);
         
         AudioClip clip = www.GetAudioClip(false);
         while(!clip.isReadyToPlay)
             yield return www;
         
         print("done loading");
         clip.name = Path.GetFileName(path);
         clips.Add(clip);
 
 
         //Count Array List Length
         //source.clip = clips[currentIndex];
         Debug.Log (clip);
         //int List = clips.Count;
         //Debug.Log (List);
     }
 
     //NEW
     void AddText()
     {
         display = currentIndex + " : " + clips[currentIndex].name;
         MusicListText.text = display;
     }
     //END NEW
 
     //Quit Button
     public void  GoQuit(){
         StartCoroutine(LoadQuit("ThreeD_Overview"));
     }
     
     IEnumerator LoadQuit(string levelMain){
         yield return new WaitForSeconds(0.5f); // wait time
         Application.LoadLevel(levelMain);
         
     }
 
 
 }
 
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

  • Sort: 
avatar image
-1

Answer by siaran · Mar 12, 2015 at 11:47 PM

Ok, 2 things:

First, there isn't a question there. Second, I had typed up an answer for the questions I think you had ('how do I shuffle' and 'how do I automatically go to the next clip'), but it struck me that it was literally 4 lines of code that you should really be able to figure out yourself if you think about your problem for more than 30 seconds.

Still, in case you really are stuck (somehow), I'll give you some hints:

For automatically playing the next item, you already have a PlayNext() method. So all you need to do is figure out when your current clip is done playing. Read the AudioSource docs.

For shuffle. Get a random value for your currentIndex then play the clip at that index.

You really should be able to figure it out with that - I've basically given the complete answers anyway even though I only wanted to hint at them.

Oh, and third, what is the point of the PlayCurrentClip() method?

Comment
Add comment · Show 2 · 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 KnightRiderGuy · Mar 15, 2015 at 01:29 PM 0
Share

lol your funny, I'm still struggling to learn the code.... better at it that I used to be but I have a LONG way to go. I can sort of see but so far none of my whacky experiments have gotten the results I was looking for.... lots of cool error messages I manage to generate that I just don't get though, lol even when it tells me the line the error is on lol ;) Secondly, not to be a dick, but this is "Unity Answers" not :Unity Hints" ;)

avatar image RabidCabbage · Mar 15, 2015 at 02:18 PM 0
Share

You need to walk before you can run.

The kind of answer you are seeking is a prime example of why posts are moderated.

Question is not specific enough: user is asking for a script, there are no suggestions on what the user has already tried.

Your task isn't a difficult one. something a beginner c# programmer should be able to work out very early into their training.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Trouble Getting Array List To Display Current Song In Game 0 Answers

Combine Children Dictionary in place of Hashtable? 2 Answers

Add value to List without replacing previous 0 Answers

Is it possible to sort an array determined by an external comparison? 1 Answer

Multiple Cars not working 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