如何从服务器中的 JSON 文件中获取音轨?
How to get Audio track from JSON file in server?
你好:) 在我的项目中,我想从 JSON 文件中获取音轨,该文件位于服务器中。
我的音频。 mp4 不想播放,我不知道为什么,我为视频制作了相同的脚本并且效果很好只是我想如果我只有声音那么它可以像视频一样工作。这是我的脚本:
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();
}
}
}
}
我看到了音频源游戏对象,但它不支持 URL 作为视频播放器游戏对象。
我希望有人帮助我。谢谢
使用第二个 UnityWebRequestMultimedia.GetAudioClip
例如
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();
}
}
}
请注意 .mp4
通常是视频格式,而不是声音格式。
你好:) 在我的项目中,我想从 JSON 文件中获取音轨,该文件位于服务器中。 我的音频。 mp4 不想播放,我不知道为什么,我为视频制作了相同的脚本并且效果很好只是我想如果我只有声音那么它可以像视频一样工作。这是我的脚本:
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();
}
}
}
}
我看到了音频源游戏对象,但它不支持 URL 作为视频播放器游戏对象。 我希望有人帮助我。谢谢
使用第二个 UnityWebRequestMultimedia.GetAudioClip
例如
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();
}
}
}
请注意 .mp4
通常是视频格式,而不是声音格式。