VideoAudioOutputMode
перечисление
Описание
Места, куда можно отправить аудио, встроенное в видео.
С помощью этого перечисления можно отключить звук, выводить его через аудиосистему Unity или направлять напрямую на звуковое оборудование.
// This script changes the audio output of a video when you press the Spacebar. // Attach this script, an AudioSource component and a VideoPlayer component to a GameObject in your Scene. // Assign a video clip to the VideoPlayer. // Edit the values of the AudioSource so that it edits the audio when it enters that mode.
using UnityEngine; using UnityEngine.Video;
public class SwitchAudioOutputMode : MonoBehaviour { VideoPlayer videoPlayer; AudioSource audioSource; private int currentAudioOutputMode = 0;
void Start() { videoPlayer = GetComponent<VideoPlayer>(); audioSource = GetComponent<AudioSource>();
if (videoPlayer == null) { Debug.LogError("No VideoPlayer assigned!"); return; }
// Set the initial audio output mode. SetAudioOutputMode(VideoAudioOutputMode.None);
videoPlayer.Play(); }
void Update() { // Press Spacebar to switch between modes if (Input.GetKeyDown(KeyCode.Space)) { CycleAudioOutputMode(); } }
private void CycleAudioOutputMode() { // Cycle through the VideoAudioOutputMode enum values. currentAudioOutputMode = (currentAudioOutputMode + 1) % System.Enum.GetValues(typeof(VideoAudioOutputMode)).Length;
// Apply the new audio output mode. SetAudioOutputMode((VideoAudioOutputMode)currentAudioOutputMode); }
private void SetAudioOutputMode(VideoAudioOutputMode audioOutputMode) { // Stop video before the audio output changes. videoPlayer.Stop(); videoPlayer.audioOutputMode = audioOutputMode;
switch (audioOutputMode) { // The video plays without audio. case VideoAudioOutputMode.None: Debug.Log("Audio Output Mode: None"); break;
// The video plays audio through an AudioSource. case VideoAudioOutputMode.AudioSource: if (audioSource == null) { Debug.LogError("AudioSource not assigned! Unable to set AudioSource mode."); return; }
// Link the VideoPlayer to the AudioSource for playback. videoPlayer.SetTargetAudioSource(0, audioSource); Debug.Log("Audio Output Mode: AudioSource"); break;
// Play the audio from the video unaltered. case VideoAudioOutputMode.Direct: Debug.Log("Audio Output Mode: Direct"); break;
case VideoAudioOutputMode.APIOnly: Debug.Log("Audio Output Mode: APIOnly (Raw audio samples exposed)"); break;
default: Debug.LogError("Unexpected Audio Output Mode!"); break; }
// Restart video playback with the new output mode. videoPlayer.Play(); } }
Свойства
| Свойство | Описание |
|---|---|
| None | Отключить встроенное аудио. |
| AudioSource | Отправить встроенное аудио в указанный AudioSource. |
| Direct | Отправка встроенного аудио напрямую на аудиоаппаратуру платформы. |
| APIOnly | Отправить встроенное аудио в ассоциированный AudioSampleProvider. |