- Home /
Could not find solution to this particular problem, alternative implementation used.
Extract audio clip from VideoPlayer for real-time processing
I have a VideoPlayer component which plays a video from a designated URL with its audio output mode set to AudioSource. I also have a script which reads an audio clip from an Audio Source and logs to the console whenever it detects a 'beat'. This AudioProcessor script works fine when I manually place an audio clip into my AudioSource, but fails when attempting to read the audio from a VideoPlayer.
I have a helper script which creates a clip and places it into the AudioSource.
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class FindAudioClip : MonoBehaviour
{
void Awake()
{
AudioSource audioSource = GetComponent<AudioSource>();
var samplesLength = AudioSettings.outputSampleRate;
audioSource.clip = AudioClip.Create("", samplesLength, 2, AudioSettings.outputSampleRate, true, null);
audioSource.loop = false;
audioSource.Play();
}
}
However, the audio clip it places into my Audio Source appears to be devoid of any sort of data. My AudioProcessor identifies that a clip is playing but does not register any beats.
For reference, here is my basic video player script which plays a video from a URL.
IEnumerator playVideoURL()
{
videoPlayer = gameObject.GetComponent<VideoPlayer>();
audioSource = gameObject.GetComponent<AudioSource>();
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
audioSource.Pause();
videoPlayer.source = VideoSource.Url;
Debug.Log(videoURL);
videoPlayer.url = videoURL;
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
videoPlayer.controlledAudioTrackCount = 1;
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
yield return null;
}
image.texture = videoPlayer.texture;
videoPlayer.Play();
audioSource.Play();
AudioProcessor.audioReady = true;
}
I know it's been a while, but I'm curious if you ever figured this out!
Follow this Question
Related Questions
Several audiosources play at the same time 1 Answer
Problem with playing sound on Trigger-Enter 2 Answers
AudioClip.Create() + PlayClipAtPoint() Not Working 1 Answer
Audio Clip Playing every frame 2 Answers
Audio Manager 1 Answer