Question by
RB_lashman · Jun 20, 2020 at 12:25 PM ·
scripting problemaudiosourceaudioclip
after adding AudioSource component through script the audio is paused
i spent the last 5h googling, trying to find a solution (or rather - trying to find out what i broke) ... and i give up ... i just don't know what's going on :(
so it did actually work, but after reshuffling some code around (and unfortunately i don't remember which change broke it) it stopped working ... the script still adds an AudioSource to the game object, it still loads the file (or at least the file appears as loaded) ... but it just doesn't play the audio (and no, i don't have the "mute" button enabled) ... the audio also shows up as "paused audio sources" in the profiler
here's my code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class Test : MonoBehaviour
{
private string _customSongsPath = "/CustomLevels/";
private List<DirectoryInfo> _customSongsList;
public AudioSource SongToPlay;
public AudioClip Song;
public SongData SongData;
private string _songPath;
private string _dataFile = "data.json";
private string _dataFilePath;
private void Awake()
{
SongToPlay = gameObject.AddComponent<AudioSource>();
DirectoryInfo dir = new DirectoryInfo(Application.dataPath + _customSongsPath);
_customSongsList = new List<DirectoryInfo>();
DirectoryInfo[] dirAll = dir.GetDirectories("*");
foreach (DirectoryInfo d in dirAll)
{
_customSongsList.Add(d);
}
_songPath = "file://" + _customSongsList[0];
_dataFilePath = _customSongsList[0] + "/" + _dataFile;
if (File.Exists(_dataFilePath))
{
string json = File.ReadAllText(_dataFilePath);
SongData = JsonUtility.FromJson<SongData>(json);
}
StartCoroutine(LoadAudio());
}
private IEnumerator LoadAudio()
{
WWW request = GetAudioFromFile(_songPath, SongData._songFilename);
yield return request;
Song = request.GetAudioClip();
Song.name = SongData._songFilename;
PlayAudioFile();
}
private void PlayAudioFile()
{
if (SongToPlay != null)
{
SongToPlay.clip = Song;
SongToPlay.Play();
}
}
private WWW GetAudioFromFile(string path, string filename)
{
string audioToLoad = string.Format(path + "{0}", filename);
WWW request = new WWW(audioToLoad);
return request;
}
}
please help :(
Comment