I can't figure out how to get the audioClip of a movieTexture in code.
I'm trying to download the audio part of a video off of youtube and I think I have to download the video part first, then the audio part, but I'm not sure. PLEASE DONT SAY: OH WELL ITS WWW.MOVIE.AUDIOCLIP because I have already tried that and it doesn't work. Here is my code:
public string YoutubeDownloadStringBoi;
public static YoutubeVideo Instance;
public GameObject camera;
void Update()
{
YoutubeDownloadStringBoi = camera.GetComponent<MovieToAudio>().enteredString;
}
void Awake()
{
Instance = this;
}
public bool drawBackground = false;
public Texture2D backgroundImage;
public string RequestVideo(string urlOrId, int quality)
{
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
Uri uriResult;
bool result = Uri.TryCreate(urlOrId, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (!result)
urlOrId = "https://youtube.com/embed/" + urlOrId;
//urlOrId = "https://youtube.com/watch?v=" + urlOrId;
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(urlOrId, false);
VideoInfo video = null;
//Search for video with desired format and desired resolution, you can filter if your own methods if you need.
var enumerator = videoInfos.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current.VideoType == VideoType.Mp4 && enumerator.Current.Resolution == quality)
{
video = enumerator.Current;
break;
}
}
if (video.RequiresDecryption)
{
DownloadUrlResolver.DecryptDownloadUrl(video);
}
Debug.Log("The mp4 is: " + video.DownloadUrl);
WWW www = new WWW(video.DownloadUrl);
StartCoroutine(DownloadYoutubeBoi(www));
return video.DownloadUrl;
}
IEnumerator DownloadYoutubeBoi(WWW www)
{
Debug.Log("inn");
yield return www;
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
}
else
{
Debug.Log("WWW Error: " + www.error);
}
camera.GetComponent<AudioSource>().clip = www.movie.audioClip;
camera.GetComponent<AudioSource>().Play();
}
Have you tried waiting until the movie is ready to play? I ask because the code example on the scripting documentation has a bit in it that waits, perhaps the audio clip is only available after waiting for the movie to be ready?
while (!www.movie.isReadyToPlay) {
yield return null;
}
https://docs.unity3d.com/ScriptReference/$$anonymous$$ovieTexture-audioClip.html
I added that to my code. It didn't work, but I wanted to tell you that it lagged my game so much it was pretty much unplayable. I waited for it to work for about 5 $$anonymous$$ and it still didn't work.
I'm afraid that's all I had. The long lag though does suggest that the video you're downloading is simply never being marked as "ready" not sure what would cause that though, format, size, etc.
Not familiar enough with $$anonymous$$ovieTextures or downloading video to go any deeper into this though, best of luck in finding a solution .