Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question after adding AudioSource component through script the audio is paused

Discussion in 'Scripting' started by RB_lashman, Jun 20, 2020.

  1. RB_lashman

    RB_lashman

    Joined:
    Aug 15, 2013
    Posts:
    92
    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:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7.  
    8. public class Test : MonoBehaviour
    9. {
    10.     private string _customSongsPath = "/CustomLevels/";
    11.     private List<DirectoryInfo> _customSongsList;
    12.     public AudioSource SongToPlay;
    13.     public AudioClip Song;
    14.     public SongData SongData;
    15.     private string _songPath;
    16.     private string _dataFile = "data.json";
    17.     private string _dataFilePath;
    18.  
    19.     private void Awake()
    20.     {
    21.         SongToPlay = gameObject.AddComponent<AudioSource>();
    22.  
    23.         DirectoryInfo dir = new DirectoryInfo(Application.dataPath + _customSongsPath);
    24.         _customSongsList = new List<DirectoryInfo>();
    25.      
    26.         DirectoryInfo[] dirAll = dir.GetDirectories("*");
    27.  
    28.         foreach (DirectoryInfo d in dirAll)
    29.         {
    30.             _customSongsList.Add(d);
    31.         }
    32.  
    33.         _songPath = "file://" + _customSongsList[0];
    34.  
    35.         _dataFilePath = _customSongsList[0] + "/" + _dataFile;
    36.  
    37.         if (File.Exists(_dataFilePath))
    38.         {
    39.             string json = File.ReadAllText(_dataFilePath);
    40.  
    41.             SongData = JsonUtility.FromJson<SongData>(json);
    42.         }
    43.  
    44.         StartCoroutine(LoadAudio());
    45.     }
    46.  
    47.     private IEnumerator LoadAudio()
    48.     {
    49.         WWW request = GetAudioFromFile(_songPath, SongData._songFilename);
    50.         yield return request;
    51.  
    52.         Song = request.GetAudioClip();
    53.         Song.name = SongData._songFilename;
    54.  
    55.         PlayAudioFile();
    56.     }
    57.  
    58.     private void PlayAudioFile()
    59.     {
    60.         if (SongToPlay != null)
    61.         {
    62.             SongToPlay.clip = Song;
    63.             SongToPlay.Play();
    64.         }
    65.     }
    66.  
    67.     private WWW GetAudioFromFile(string path, string filename)
    68.     {
    69.         string audioToLoad = string.Format(path + "{0}", filename);
    70.         WWW request = new WWW(audioToLoad);
    71.         return request;
    72.     }
    73. }
    74.  




    please help :(
     
unityunity