we struggled for a bit but figured out in the end. but with a round about way I guess. it's our first time for me and team members to work with VR so there's a probably better way than this :). Any help would be appreciated.
here is the new code my college made.
public class MidiFileLoader : MonoBehaviour { public List midiFilePaths = new List(); // Store all MIDI file paths public GameObject buttonPrefab; // Assign a UI Button prefab with a Text component public Transform buttonContainer; // A parent object in the UI where buttons will be placed public string midiListFileName = "midi_list.txt"; // The text file that lists all MIDI files
// UI elements for progress and debugging
public Slider downloadProgressSlider; // Progress bar for file download
public TMP_Text debugText; // TextMeshPro text for debugging and showing progress
void Start()
{
// Request permission for external storage on Android
if (Application.platform == RuntimePlatform.Android)
{
RequestStoragePermissions();
}
// Start loading MIDI file list
StartCoroutine(LoadMidiFileList());
}
// Coroutine to load the list of MIDI files from StreamingAssets
private IEnumerator LoadMidiFileList()
{
string midiListPath = Path.Combine(Application.streamingAssetsPath, "MidiFiles", midiListFileName);
// Use UnityWebRequest to load the list file (for Android and other platforms)
UnityWebRequest request = UnityWebRequest.Get(midiListPath);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
// Parse the file list (assuming each file is on a new line)
string fileList = request.downloadHandler.text;
string[] lines = fileList.Split('\n');
foreach (string line in lines)
{
if (!string.IsNullOrEmpty(line.Trim()))
{
midiFilePaths.Add(line.Trim());
}
}
// Populate the UI with buttons for each MIDI file
PopulateMenu();
}
else
{
Debug.LogError("Failed to load MIDI list: " + request.error);
}
}
// This method will populate the menu with the MIDI file paths
void PopulateMenu()
{
for (int i = 0; i < midiFilePaths.Count; i++)
{
string path = midiFilePaths[i];
// Instantiate the button inside the buttonContainer
GameObject newButton = Instantiate(buttonPrefab, buttonContainer);
// Set the button text to the MIDI file name
newButton.GetComponentInChildren<TMP_Text>().text = Path.GetFileNameWithoutExtension(path);
// Add a listener to the button to load the selected file when clicked
Button buttonComponent = newButton.GetComponent<Button>();
string fullPath = Path.Combine(Application.streamingAssetsPath, "MidiFiles", path);
buttonComponent.onClick.AddListener(() =>
{
Debug.Log("Button clicked for: " + fullPath);
StartCoroutine(LoadAndPlayMidi(fullPath));
});
}
}
// Coroutine to load and play a MIDI file
private IEnumerator LoadAndPlayMidi(string filePath)
{
// Use UnityWebRequest to access the file in StreamingAssets
UnityWebRequest request = UnityWebRequest.Get(filePath);
// Reset progress bar and debug text
downloadProgressSlider.value = 0;
debugText.text = "Starting download...";
request.SendWebRequest();
// Update the progress bar while downloading
while (!request.isDone)
{
downloadProgressSlider.value = request.downloadProgress;
debugText.text = $"Downloading... {request.downloadProgress * 100}%";
yield return null; // Wait until the next frame
}
if (request.result == UnityWebRequest.Result.Success)
{
// Save the downloaded file to persistentDataPath
string tempFilePath = Path.Combine(Application.persistentDataPath, Path.GetFileName(filePath));
// Write the downloaded data to a local file
File.WriteAllBytes(tempFilePath, request.downloadHandler.data);
// Call MidiReader with the file path of the saved file
MidiReader.instance.StartReading(tempFilePath);
// Debug: Notify user that the file is ready
debugText.text = "Download complete!";
}
else
{
Debug.LogError("Failed to load MIDI file: " + request.error);
debugText.text = "Download failed: " + request.error;
}
// Reset progress bar after download
downloadProgressSlider.value = 0;
}
// Request external storage permissions on Android
private void RequestStoragePermissions()
{
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
{
Permission.RequestUserPermission(Permission.ExternalStorageRead);
}
// Optional: Check for write permission if needed
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}
}
}