Unity 2D 中的视频播放器播放音频但在处于非活动状态后设置为活动时不显示视觉效果

Posted

技术标签:

【中文标题】Unity 2D 中的视频播放器播放音频但在处于非活动状态后设置为活动时不显示视觉效果【英文标题】:Video Player in Unity 2D playing audio but not showing visual when setting active after being inactive 【发布时间】:2020-07-21 10:07:00 【问题描述】:

我没有经常使用 Unity,我正在尝试制作一个游戏,它在后台播放视频,然后删除视频,然后再次继续播放视频(继续遵循此模式),看看是否播放器的分数会受到在后台播放视频的影响。

我在带有原始图像的面板上有视频播放器脚本,当前的问题是,在视频播放 15 秒后,我使面板处于非活动状态,当我再次使其处于活动状态时,视频中的音频播放,但视频不出现。此外,它每次都会重新启动视频,但我希望它从中断的地方继续播放。我尝试使用 videoPlayer.Pause,但我不希望视频仍然在背景中可见。

任何建议都会很有帮助!

这是我的 videoPlayer 脚本:

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

public class streamVideo : MonoBehaviour

    public RawImage image;

    //public VideoClip videoToPlay;

    private VideoPlayer videoPlayer;
    private VideoSource videoSource;
    private VideoScript script;

    private Audiosource audioSource;
    // Start is called before the first frame update
    void Start()
    
            Application.runInBackground = true;
            StartCoroutine(playVideo());
    

IEnumerator playVideo()
    
        videoPlayer = gameObject.AddComponent<VideoPlayer>();
        if (audioToggle.audioBool)
        
            audioSource = gameObject.AddComponent<AudioSource>();
            audioSource.playOnAwake = false;
            audioSource.Pause();
        



        videoPlayer.playOnAwake = false;

        videoPlayer.source = VideoSource.Url;
        videoPlayer.url = VideoScript.myPath;
        videoPlayer.isLooping = true;

        //Set Audio Output to AudioSource
        videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

        //Assign the Audio from Video to AudioSource to be played
        videoPlayer.EnableAudioTrack(0, true);
        videoPlayer.SetTargetAudioSource(0, audioSource);

        //Set video To Play then prepare Audio to prevent Buffering
        //videoPlayer.clip = videoToPlay;
        videoPlayer.Prepare();

        //Wait until video is prepared
        while (!videoPlayer.isPrepared)
        
            yield return null;
        

        //Assign the Texture from Video to RawImage to be displayed
        image.texture = videoPlayer.texture;


            videoPlayer.Play();


            //if game audio is on
            if (audioToggle.audioBool)
            
                //Play Sound
                audioSource.Play();
            

    

    // Update is called once per frame
    void Update()
    

    

这是我的计时器脚本,它设置了视频面板:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class timeScript : MonoBehaviour

    public GameObject videoPanel;
    public float mediaTime = 0;


    // Use this for initialization
    void Start()
    

    

    // Update is called once per frame
    void Update()
    

        mediaTime += Time.deltaTime;

        if (VideoScript.isVideo & mediaTime >= Random.Range(10,20))
//VideoScript.isVideo is just a bool that checks whether a video was loaded by the player
        
            videoPanel.SetActive(!videoPanel.activeSelf);
            mediaTime = 0;


        

【问题讨论】:

【参考方案1】:

您可以暂停视频并隐藏网格渲染器组件。

videoPlayer.Pause();
videoPlayer.gameObject.GetComponent<MeshRenderer>().enabled = false;
...
videoPlayer.Play();
videoPlayer.gameObject.GetComponent<MeshRenderer>().enabled = true;

【讨论】:

以上是关于Unity 2D 中的视频播放器播放音频但在处于非活动状态后设置为活动时不显示视觉效果的主要内容,如果未能解决你的问题,请参考以下文章

视频不在设备上播放,但在播放 ov 模拟器

如果背景音频处于活动状态,MPMoviePlayerController 将冻结

在 Agora.io 视频通话后 Unity VideoPlayer 音频中断

Unity - 音频文件在 iPhone 和 iPad 上播放不佳

在 div 中播放音频

即使手机处于静音或振动状态,如何让视频播放声音? [复制]