Unity Video脚本无法播放声音

时间:2017-09-13 23:14:27

标签: unity3d audio video

我正在使用统一资产商店的插件并尝试播放带有声音的视频 - 目前不是。以下是播放视频的脚本 -

using UnityEngine;
using UnityEngine.Video;

namespace Complete360Tour {
    [AddComponentMenu("Complete360Tour/Media/VideoMediaReactor")] public class VideoMediaReactor : MonoBehaviour, IMediaSwitchReactor<VideoMediaNodeData> {
        //-----------------------------------------------------------------------------------------
        // Inspector Variables:
        //-----------------------------------------------------------------------------------------

        [Header("Assignment")] [SerializeField] protected MediaView mediaView;
        [SerializeField] protected VideoPlayer videoPlayer;

        //-----------------------------------------------------------------------------------------
        // Private Fields:
        //-----------------------------------------------------------------------------------------

        private RenderTexture renderTexture;

        //-----------------------------------------------------------------------------------------
        // Unity Lifecycle:
        //-----------------------------------------------------------------------------------------

        protected void Awake() {
            if (mediaView == null) Debug.LogWarning("No MediaView assigned. Please assign a MediaView.");
        }

        //-----------------------------------------------------------------------------------------
        // Public Methods:
        //-----------------------------------------------------------------------------------------

        public void SwitchMedia(VideoMediaNodeData data, MediaSwitchStates state) {
            if (data == null) {
                InvalidSwitchData();
                return;
            }

            switch (state) {
                case MediaSwitchStates.BeforeSwitch: break;
                case MediaSwitchStates.Switch:
                    BeginVideo(data.VideoClip);
                    mediaView.SetStereoscopic(data.IsStereo);
                    break;
                case MediaSwitchStates.AfterSwitch: break;
            }
        }

        public void ExitMedia() { InvalidSwitchData(); }

        //-----------------------------------------------------------------------------------------
        // Private Methods:
        //-----------------------------------------------------------------------------------------

        private void InvalidSwitchData() {
            videoPlayer.Stop();
            videoPlayer.targetTexture = null;

            mediaView.SetMedia(null);
            mediaView.SetStereoscopic(false);
        }

        private void BeginVideo(VideoClip videoClip) {
            renderTexture = new RenderTexture((int) videoClip.width, (int) videoClip.height, 0);
            videoPlayer.clip = videoClip;
            videoPlayer.targetTexture = renderTexture;
            mediaView.SetMedia(renderTexture);
            videoPlayer.Play();
        }
    }
}

我尝试添加

之后的音频内容

http://justcode.me/unity2d/how-to-play-videos-on-unity-using-new-videoplayer/

但没有运气。音频仍然无法播放。

1 个答案:

答案 0 :(得分:0)

如果您使用的是Unity Video Player:

  1. 尝试将音频源组件添加到持有视频播放器的GameObject
  2. 将音频源拖放到视频播放器组件中的音频源