- Home /
Input Field Text string to string variable. (const string)
I have this script where you can play an Audio an .ogg to be exact because what I'm doing is getting an audio from my computers directory, Im doing this because I need an "audio file browser" in unity runtime but I have yet to find any suitable tutorials. so what I can do for now is create an Input field and there we would put the directory of the .ogg file(unity does not recognize mp3, they say unity actually converts mp3 file to .ogg when you import them) so anyway this is the script.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
[RequireComponent(typeof(AudioSource))]
public class MP3AudioImporter : MonoBehaviour {
private List<AudioClip> audioClips;
private const string musicDir = "C:\\Users\\Username\\Music";
public InputField x;
void Start()
{
audioClips = new List<AudioClip>();
StartCoroutine("PlayAudioList");
}
IEnumerator DownloadPlaylist()
{
string[] playlist = Directory.GetFiles (@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);
foreach(string song in playlist)
{
WWW audioLoader = new WWW("file://" + song);
while( !audioLoader.isDone )
yield return null;
audioClips.Add(audioLoader.GetAudioClip(false));
}
}
IEnumerator PlayAudioList()
{
yield return StartCoroutine("DownloadPlaylist");
foreach(AudioClip song in audioClips)
{
GetComponent<AudioSource>().clip = song;
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(song.length + 1.0f);
}
}
}
How can I make the string in InputField x as a const string variable in musicDir? actually any good idea for an Audio File Browser would be good too. and the way you can upload mp3 without converting it to .ogg in some 3rd party.
Your answer
Follow this Question
Related Questions
Text UI is not being assigned 2 Answers
string + int = variable 2 Answers
Change multiple variables with function argument 2 Answers
Public variable hidden in the inspector 2 Answers