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. Dismiss Notice

Question Downloaded audio fails to play

Discussion in 'Audio & Video' started by rneupane, Feb 7, 2022.

  1. rneupane

    rneupane

    Joined:
    Jan 15, 2022
    Posts:
    1
    Hi. I'm new to Unity and looking for some help.

    I am saving system.io.stream as a mp3 file and trying to play the audio right after. The code runs fine with no error audio does not play. I think it is being triggered to play before the download is complete / object is created. I'm able to run it fine with a file that already exist.

    Code (CSharp):
    1. using (var fileStream = File.Create(audioPath))
    2.                     {
    3.                         audioAPIResponse.AudioStream.CopyTo(fileStream);
    4.                         fileStream.Flush();
    5.                         fileStream.Close();
    6.                         var playClip = Resources.Load<AudioClip>(filename);
    7.                         SoundManager.Instance.PlaySound(playClip);
    8.                     }
    audioPath = Resources/<epoch_time>.mp3
    filename = <epoch_time>


    SoundManager script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SoundManager : MonoBehaviour
    6. {
    7.     public static SoundManager Instance;
    8.     [SerializeField] private AudioSource _musicSource;
    9.  
    10.     void Awake() {
    11.         if (Instance == null) {
    12.             Instance = this;
    13.             DontDestroyOnLoad(gameObject);
    14.         }
    15.         else {
    16.             Destroy(gameObject);
    17.         }
    18.     }
    19.  
    20.     public void PlaySound(AudioClip clip) {
    21.         _musicSource.PlayOneShot(clip);
    22.     }
    23. }
    24.  
    What am I doing wrong? Any way to improve this flow? Maybe not save as file but play the stream and discard?
     
  2. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    498
    Sorry for the long delay. Everything coming through Resource.Load has to be imported in the Unity Editor and then "built into" in the final game. But you can still achieve what you need by using UnityWebRequestMultimedia.GetAudioClip. Here is an example script that plays a file at runtime.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4.  
    5. public class LoadRuntimeAudio : MonoBehaviour
    6. {
    7.     public string path;     //Absolute path like C:\Users\Name\Desktop\music.mp3
    8.     public AudioType type;
    9.  
    10.     public AudioSource source;
    11.  
    12.     void Start()
    13.     {
    14.         StartCoroutine(GetAndPlayAudioClip());
    15.     }
    16.  
    17.     IEnumerator GetAndPlayAudioClip()
    18.     {
    19.         using (var uwr = UnityWebRequestMultimedia.GetAudioClip(path, type))
    20.         {
    21.             ((DownloadHandlerAudioClip)uwr.downloadHandler).streamAudio = true;
    22.  
    23.             yield return uwr.SendWebRequest();
    24.  
    25.             if (uwr.isNetworkError || uwr.isHttpError)
    26.             {
    27.                 Debug.LogError(uwr.error);
    28.                 yield break;
    29.             }
    30.  
    31.             AudioClip myClip = DownloadHandlerAudioClip.GetContent(uwr);
    32.  
    33.             source.clip = myClip;
    34.             source.Play();
    35.         }
    36.     }
    37. }
    38.  
     
  3. SolomonPeters

    SolomonPeters

    Joined:
    Apr 22, 2022
    Posts:
    1
    I want to download ringtone and use it
    I can't hear it when I download it and I have a ringtone set on my phone
    what is that error?
     
  4. nthmusic51

    nthmusic51

    Joined:
    Friday
    Posts:
    1
    Oh, maybe you've downloaded ringtones from places that aren't of good quality, that happened to me too. You should download from tonosdellamadacanciones, they always have high quality ringtones and are updated very often, I only download them here.