首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用统一视频播放器无缝地播放不同的视频

使用统一视频播放器无缝地播放不同的视频
EN

Stack Overflow用户
提问于 2017-11-20 21:54:39
回答 1查看 13.4K关注 0票数 3

有没有人有任何解决这个问题的统一的视频播放器组件?

基本上,我有一个半交互式的应用程序,当事情发生时,它会背靠背地播放一系列视频。问题是,一旦第一个视频结束,就会有一个很好的3-5秒间隔,因为第二个视频需要时间来加载,但是在我的应用程序中,这看起来不太好。

我需要一种方式,要么预装第二个视频(理想)或相当简单的方式隐藏差距(如静止帧顶部)。对于后一种想法,我应该提到这些视频都是球形的。

代码语言:javascript
复制
using UnityEngine;
using UnityEngine.Video;

public class selectAndPlay_video : MonoBehaviour {

    public VideoPlayer videoPlayer;
    public VideoClip NewClip;
    void OnEnable()
    {
        videoPlayer.loopPointReached += loopPointReached;
    }

    void OnDisable()
    {
        videoPlayer.loopPointReached -= loopPointReached;
    }

   void loopPointReached(VideoPlayer v)

    {
       videoPlayer.clip = NewClip;
       videoPlayer.Play();
     }
}
EN

回答 1

Stack Overflow用户

发布于 2017-11-21 14:27:35

解决这个问题的关键是VideoPlayer.Prepare()函数、VideoPlayer.timeVideoPlayer.clip.length属性。首先,我建议你忘记当前播放视频的方式。使用coroutine,正如this问题中的示例所做的那样,因为下面答案中的所有内容都假设您处于协同函数中。下面是如何播放不同的视频,而不需要等待很长时间:

1.Have数组/要播放的VideoClip列表。

2.Create来自#1VideoClip数组的新VideoPlayer列表。只需使用来自VideoPlayer.clip #1的VideoClips设置属性。

3.Call VideoPlayer.Prepare()首先在列表中准备 VideoPlayer,然后在while循环中等待,直到准备完成或VideoPlayer.isPrepared变为true

4.Call VideoPlayer.Play()播放您刚刚在#3中准备的视频。

连续播放不同的视频。

这是最重要的部分。

播放视频时,请检查正在播放的VideoPlayer的当前时间是否为视频长度的一半。如果是一半,在数组中的下一个视频上调用VideoPlayer.Prepare(),然后等待来自#4的视频在播放下一个视频之前完成播放。

通过这样做,下一个视频将开始准备自己,而当前的视频仍在播放,准备过程应在当前视频完成播放之前完成。然后,您可以播放下一个视频,而不等待它加载很长时间。

5.Wait从#4获得视频,在while循环中完成播放,直到VideoPlayer.isPlaying变成false为止。

6.While在#5中的while循环中等待,检查视频是否与if (videoPlayerList[videoIndex].time >= (videoPlayerList[videoIndex].clip.length / 2))播放了一半。如果这是真的,请调用列表中下一个VideoPlayer上的VideoPlayer以保持其就绪。

7.After while循环存在,当前视频正在播放。播放列表中的下一个VideoPlayer,然后从#5中再次重复,以准备列表中的下一个VideoPlayer

下面的脚本应该完成我上面描述的所有事情。只需创建一个RawImage并将其插入图像插槽即可。您所有的视频都到了videoClipList变量。它应该一个一个地播放视频,而不需要很长的加载时间。Debug.Log很昂贵,所以一旦您验证它是否正常工作,就将它们删除。

代码语言:javascript
复制
//Raw Image to Show Video Images [Assign from the Editor]
public RawImage image;
//Set from the Editor
public List<VideoClip> videoClipList;

private List<VideoPlayer> videoPlayerList;
private int videoIndex = 0;


void Start()
{
    StartCoroutine(playVideo());
}

IEnumerator playVideo(bool firstRun = true)
{
    if (videoClipList == null || videoClipList.Count <= 0)
    {
        Debug.LogError("Assign VideoClips from the Editor");
        yield break;
    }

    //Init videoPlayerList first time this function is called
    if (firstRun)
    {
        videoPlayerList = new List<VideoPlayer>();
        for (int i = 0; i < videoClipList.Count; i++)
        {
            //Create new Object to hold the Video and the sound then make it a child of this object
            GameObject vidHolder = new GameObject("VP" + i);
            vidHolder.transform.SetParent(transform);

            //Add VideoPlayer to the GameObject
            VideoPlayer videoPlayer = vidHolder.AddComponent<VideoPlayer>();
            videoPlayerList.Add(videoPlayer);

            //Add AudioSource to  the GameObject
            AudioSource audioSource = vidHolder.AddComponent<AudioSource>();

            //Disable Play on Awake for both Video and Audio
            videoPlayer.playOnAwake = false;
            audioSource.playOnAwake = false;

            //We want to play from video clip not from url
            videoPlayer.source = VideoSource.VideoClip;

            //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 Clip To Play 
            videoPlayer.clip = videoClipList[i];
        }
    }

    //Make sure that the NEXT VideoPlayer index is valid
    if (videoIndex >= videoPlayerList.Count)
        yield break;

    //Prepare video
    videoPlayerList[videoIndex].Prepare();

    //Wait until this video is prepared
    while (!videoPlayerList[videoIndex].isPrepared)
    {
        Debug.Log("Preparing Index: " + videoIndex);
        yield return null;
    }
    Debug.LogWarning("Done Preparing current Video Index: " + videoIndex);

    //Assign the Texture from Video to RawImage to be displayed
    image.texture = videoPlayerList[videoIndex].texture;

    //Play first video
    videoPlayerList[videoIndex].Play();

    //Wait while the current video is playing
    bool reachedHalfWay = false;
    int nextIndex = (videoIndex + 1);
    while (videoPlayerList[videoIndex].isPlaying)
    {
        Debug.Log("Playing time: " + videoPlayerList[videoIndex].time + " INDEX: " + videoIndex);

        //(Check if we have reached half way)
        if (!reachedHalfWay && videoPlayerList[videoIndex].time >= (videoPlayerList[videoIndex].clip.length / 2))
        {
            reachedHalfWay = true; //Set to true so that we don't evaluate this again

            //Make sure that the NEXT VideoPlayer index is valid. Othereise Exit since this is the end
            if (nextIndex >= videoPlayerList.Count)
            {
                Debug.LogWarning("End of All Videos: " + videoIndex);
                yield break;
            }

            //Prepare the NEXT video
            Debug.LogWarning("Ready to Prepare NEXT Video Index: " + nextIndex);
            videoPlayerList[nextIndex].Prepare();
        }
        yield return null;
    }
    Debug.Log("Done Playing current Video Index: " + videoIndex);

    //Wait until NEXT video is prepared
    while (!videoPlayerList[nextIndex].isPrepared)
    {
        Debug.Log("Preparing NEXT Video Index: " + nextIndex);
        yield return null;
    }

    Debug.LogWarning("Done Preparing NEXT Video Index: " + videoIndex);

    //Increment Video index
    videoIndex++;

    //Play next prepared video. Pass false to it so that some codes are not executed at-all
    StartCoroutine(playVideo(false));
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47401759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档