VideoPlayer的VideoClp的音频源的音量控制

时间:2019-05-15 15:35:56

标签: unity3d audio video-player

我试图为视频播放器设置一个音量控制滑块,但是很难弄清楚如何在Unity Video Player中控制视频剪辑的音量。我似乎无法将音频源链接到视频剪辑,因此可以控制音量。我对单个mp3音频文件没有任何问题,但无法使其与视频文件一起使用。

有什么建议吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.EventSystems;

public class track : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{

    public AudioSource audio;
    public Slider audiovolume;

    public VideoPlayer video;
    Slider tracking;
    bool slide = false;

    void Start()
    {

        tracking = GetComponent<Slider>();

    }

    public void OnPointerDown(PointerEventData a)
    {

        slide = true;
    }

    public void OnPointerUp(PointerEventData a)
    {

        float frame = (float)tracking.value * (float)video.frameCount;
        video.frame = (long)frame;
        slide = false;

    }

    void Update()
    {

        if (!slide && video.isPlaying)

            tracking.value = (float)video.frame / (float)video.frameCount;
    }

    public void volume()
    {

        audio.volume = audiovolume.value;

    }

} 

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码似乎没有以任何方式链接音频音量滑块。 试试这个:

void Start()
{

    tracking = GetComponent<Slider>();
    if (audiovolume!=null && audio!=null) 
    audiovolume.onValueChanged.AddListener((x)=>audio.volume=x);
}