How to get Audio track from JSON file in server?

598 Views Asked by At

Hello :) in my Project I want to get Audio track from a JSON file file which locate it in a Server. my Audio. mp4 doesn't want to play, I don't know why ,, I made the same script for video and it works good just I thought if I have only the sound so it can work the same way as a video. this is my script:

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Collections;
using TMPro;
using UnityEngine.Video;

    // Json dataaudio format
    /*
          {
            "Title"     : "..." ,
            "AudioURL" : "..."
          }
    */
    public struct Dataudio
    {
        public string Title;
        public string AudioURL;
    }
    
    public class getaudio : MonoBehaviour
    {
        [SerializeField] TextMeshPro TitleText;
        [SerializeField] private VideoPlayer videoPlayer;
        [SerializeField] private RawImage rawImage;
    
       
    
        string jsonURL = "https://myserver";
    
        IEnumerator Start()
        {
            using (var request = UnityWebRequest.Get(jsonURL)) 
            {
                yield return request.SendWebRequest();
    
                if (request.isNetworkError || request.isHttpError)
                {
                    // error ...
                }
                else
                {
                    // success...
                    Dataaudio data = JsonUtility.FromJson<Dataaudio>(request.downloadHandler.text);
    
                    // print data in UI
                    uiTitleText.text = data.Title;
    
                    // The video player will take care of loading the video clip all by itself!
                    videoPlayer.url = data.AudioURL;
                    videoPlayer.Play();
                }
    
            }
    
        }
    
    }

I saw the Audio Source Game object but it doesn't support URL as Video player game object. I hope someone help me. Thank you

1

There are 1 best solutions below

0
On BEST ANSWER

By using a second UnityWebRequestMultimedia.GetAudioClip e.g.

    IEnumerator Start()
    {
        Dataaudio data;
        using (var request = UnityWebRequest.Get(jsonURL)) 
        {
            yield return request.SendWebRequest();

            if (request.isNetworkError || request.isHttpError)
            {
                // error ...

                yield break;
            }
            else
            {
                // success...
                data = JsonUtility.FromJson<Dataaudio>(request.downloadHandler.text);
            }
        }

        // print data in UI
        uiTitleText.text = data.Title;

        using (var clipRequest = UnityWebRequestMultimedia.GetAudioClip(data.AudioURL, AudioType.WAV /*TODO use correct audio type here*/)) 
        {
            yield return clipRequest.SendWebRequest();

            if (clipRequest.isNetworkError || clipRequest.isHttpError)
            {
                // error ...

                yield break;
            }
            else
            {
                // success...
                var clip = DownloadHandlerAudioClip.GetContent(www);

                someAudioSource.clip = clip;
                someAudioSource.Play();
            }
        }
    }

Note though that .mp4 is usually a video format, not a sound format.