- Home /
Solution Was posted
Trouble Getting Array List To Display Current Song In Game
How do I get this array list of my loaded in music to display a simple list of the songs in the array/list in my scene with UI text. Here is the script I have been trying many experiments on but getting nowhere with. It seems I can get the clip count to display in the console but I need my UI text to display the list of songs in the array, or at the very least display the name of the current song playing. you would think this would be an easy one for you code guys??
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;
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/UserAudio"; // 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/UserAudio";
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();
}
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 (NOTE to Dean: Michael's Edit to see if I can get list count to display in the console... It works :)
//We somehow need to display our list of music from the array list???
int List = clips.Count;
Debug.Log (List);
}
//NEW
void AddText()
{
{
display = display.ToString () + clips.Count.ToString();
}
MusicListText.text = display;
}
//END NEW
}
Function void AddText() just displays the size of array. $$anonymous$$aybe it should be like:
foreach(var m in this.clips){
$$anonymous$$usicListText.text += m.ToString() + "\n";
}
Thanks for responding NoxCaos. Well, it does not give me any errors but it does not do anything with my UI text as far as making it display my music that has populated the array??
I thinks part of the answer is in this line: But I'm not totally sure.??
StartCoroutine(LoadFile(s.FullName));
Okay, simple code snip that should display all your available clips on initialization
public AudioClip[] availableClips;
//You might want to use a TextArea ins$$anonymous$$d of a Text object here
Text allClips;
void Start(){
foreach(Audioclip c in availableClips){
allClips.text += c.name + "\n"
}
}
not sure if an AudioClip has a name that you can call like that but it seems like it based on the above comments. Anyway, seriously try that, there is no reason (besides AudioClips not having a name) why it shouldn't work.