Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用新的Unity VideoPlayer和VideoClip API播放视频_C#_Unity3d_Unity5 - Fatal编程技术网

C# 使用新的Unity VideoPlayer和VideoClip API播放视频

C# 使用新的Unity VideoPlayer和VideoClip API播放视频,c#,unity3d,unity5,C#,Unity3d,Unity5,在Unity 5.6.0b1发布后,最终被弃用,并且在桌面和移动设备上播放视频的新API现在发布 如果需要的话,可以用来播放视频和检索每一帧的纹理 我已经设法让视频正常工作,但无法从Windows10的编辑器中播放音频。有人知道为什么不播放音频吗 //Raw Image to Show Video Images [Assign from the Editor] public RawImage image; //Video To Play [Assign from the Editor] publ

在Unity 5.6.0b1发布后,最终被弃用,并且在桌面和移动设备上播放视频的新API现在发布

如果需要的话,可以用来播放视频和检索每一帧的纹理

我已经设法让视频正常工作,但无法从Windows10的编辑器中播放音频。有人知道为什么不播放音频吗

//Raw Image to Show Video Images [Assign from the Editor]
public RawImage image;
//Video To Play [Assign from the Editor]
public VideoClip videoToPlay;

private VideoPlayer videoPlayer;
private VideoSource videoSource;

//Audio
private AudioSource audioSource;

// Use this for initialization
void Start()
{
    Application.runInBackground = true;
    StartCoroutine(playVideo());
}

IEnumerator playVideo()
{
    //Add VideoPlayer to the GameObject
    videoPlayer = gameObject.AddComponent<VideoPlayer>();

    //Add AudioSource
    audioSource = gameObject.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 video To Play then prepare Audio to prevent Buffering
    videoPlayer.clip = videoToPlay;
    videoPlayer.Prepare();

    //Wait until video is prepared
    while (!videoPlayer.isPrepared)
    {
        Debug.Log("Preparing Video");
        yield return null;
    }

    Debug.Log("Done Preparing Video");

    //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);

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

    //Play Video
    videoPlayer.Play();

    //Play Sound
    audioSource.Play();

    Debug.Log("Playing Video");
    while (videoPlayer.isPlaying)
    {
        Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
        yield return null;
    }

    Debug.Log("Done Playing Video");
}
//显示视频图像的原始图像[从编辑器分配]
公众形象;
//要播放的视频[从编辑器分配]
公共视频剪辑视频播放;
私人视频播放器;
私有视频源;
//音频
私人音频源音频源;
//用于初始化
void Start()
{
Application.runInBackground=true;
开始例行程序(播放视频());
}
IEnumerator播放视频()
{
//将VideoPlayer添加到游戏对象
videoPlayer=gameObject.AddComponent();
//添加音频源
audioSource=gameObject.AddComponent();
//禁用视频和音频唤醒时播放
videoPlayer.playOnAwake=false;
audioSource.playOnAwake=false;
//我们想从视频剪辑而不是从url播放
videoPlayer.source=VideoSource.VideoClip;
//将视频设置为播放,然后准备音频以防止缓冲
videoPlayer.clip=videoToPlay;
videoPlayer.Prepare();
//等待视频准备好
而(!videoPlayer.isPrepared)
{
调试日志(“准备视频”);
收益返回空;
}
Debug.Log(“完成准备视频”);
//将音频输出设置为AudioSource
videoPlayer.audioOutputMode=VideoAudioOutputMode.AudioSource;
//将音频从视频分配到要播放的音频源
videoPlayer.EnableAudioTrack(0,真);
videoPlayer.SetTargetAudioSource(0,音频源);
//将纹理从视频指定给要显示的原始图像
image.texture=videoPlayer.texture;
//播放视频
视频播放器;
//播放声音
audioSource.Play();
Debug.Log(“播放视频”);
while(videoPlayer.isPlaying)
{
LogWarning(“视频时间:+Mathf.floorPoint((float)videoPlayer.Time));
收益返回空;
}
Log(“播放视频完成”);
}

找到了问题。下面是播放视频和音频的固定代码:

//Raw Image to Show Video Images [Assign from the Editor]
public RawImage image;
//Video To Play [Assign from the Editor]
public VideoClip videoToPlay;

private VideoPlayer videoPlayer;
private VideoSource videoSource;

//Audio
private AudioSource audioSource;

// Use this for initialization
void Start()
{
    Application.runInBackground = true;
    StartCoroutine(playVideo());
}

IEnumerator playVideo()
{
    //Add VideoPlayer to the GameObject
    videoPlayer = gameObject.AddComponent<VideoPlayer>();

    //Add AudioSource
    audioSource = gameObject.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 To Play then prepare Audio to prevent Buffering
    videoPlayer.clip = videoToPlay;
    videoPlayer.Prepare();

    //Wait until video is prepared
    while (!videoPlayer.isPrepared)
    {
        Debug.Log("Preparing Video");
        yield return null;
    }

    Debug.Log("Done Preparing Video");

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

    //Play Video
    videoPlayer.Play();

    //Play Sound
    audioSource.Play();

    Debug.Log("Playing Video");
    while (videoPlayer.isPlaying)
    {
        Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
        yield return null;
    }

    Debug.Log("Done Playing Video");
}
必须在
videoPlayer.Prepare()之前调用不在后面。这是花了几个小时的实验才发现这是我遇到的问题


卡在“准备视频”上?

videoPlayer.Prepare()之后等待5秒钟,然后退出while循环

替换:

while (!videoPlayer.isPrepared)
{
    Debug.Log("Preparing Video");
    yield return null;
}
//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;
与:

这应该可以工作,但当视频开始播放时,您可能会遇到缓冲。在使用此临时修复程序时,我的建议是使用标题为“videoPlayer.isPrepared always true”的bug文件,因为这是一个bug

有些人还通过更改以下内容来修复此问题:

videoPlayer.playOnAwake = false; 
audioSource.playOnAwake = false;


从URL播放视频:

//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);
string url = "file://" + Application.streamingAssetsPath + "/" + "VideoName.mp4";

if !UNITY_EDITOR && UNITY_ANDROID
    url = Application.streamingAssetsPath + "/" + "VideoName.mp4";
#endif

//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = url;
替换:

while (!videoPlayer.isPrepared)
{
    Debug.Log("Preparing Video");
    yield return null;
}
//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;
与:

然后删除:

公共视频剪辑视频播放
videoPlayer.clip=videoToPlay因为这些不再需要了

从StreamingAssets文件夹播放视频:

//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);
string url = "file://" + Application.streamingAssetsPath + "/" + "VideoName.mp4";

if !UNITY_EDITOR && UNITY_ANDROID
    url = Application.streamingAssetsPath + "/" + "VideoName.mp4";
#endif

//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = url;

所有支持的视频格式

  • ogv
  • vp8
  • webm
  • 莫夫
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg
Windows上额外支持的视频格式

  • 阿维
  • asf
  • wmf

有些格式在某些平台上不起作用。有关支持的视频格式的更多信息,请参阅帖子。

类似于其他答案所说的内容。您可以在准备和结束视频状态时使用回调。而不是使用协程和收益率

videoPlayer.loopPointReached += EndReached;
videoPlayer.prepareCompleted += PrepareCompleted;

void PrepareCompleted(VideoPlayer vp) {
    vp.Play();
}

void EndReached(VideoPlayer vp) {
    // do something
}

到现在为止,视频播放器应该已经足够更新了,您不需要编写代码就可以正常工作。以下是我发现最理想效果的设置:

这些设置是:

视频播放器

  • 清醒时播放:正确
  • 等待第一帧:False
  • 音频输出模式:
音频源

  • 清醒时播放:正确

不要忘记为视频播放器准备一个视频剪辑,为音频源准备一个音频剪辑。我发现最有效的文件格式是.ogv(视频)和.wav(音频)。

我使用@Programmer的答案从URL播放视频,但我无法播放任何声音。最终,我在一本书中找到了答案

要获得通过URL加载的电影的音频播放,需要在调用
EnableAudioTrack
之前添加以下行:

videoPlayer.controlledAudioTrackCount = 1;

“你自己也皈依社区了吗?”莱斯特我没有,但如果可以的话我会的。这看起来像是因为这个问题被新用户滥用,他们只把问题作为答案发布。他不得不删除其中的多个答案/问题。我已经制作了一个关于使用VideoPlayer的视频教程。这是链接。此外,如果您正在加载多个视频,并且在应用程序中面临延迟,这可能会有所帮助。似乎VideoPlayer.prepare()在主线程上运行!尤其是在Android上。结果是一个应用程序在prepare()工作时挂起!撕开这个新界面几乎无法使用。你也不能在bckd线程上运行它。有人找到工作了吗?很抱歉不时戳你一下,但对我来说,视频没有播放,这是因为
videoPlayer.playOnAwake=false;audioSource.playOnAwake=false我将值更改为
true
。我现在明白了,我想我现在必须开发脚本来控制视频的播放和暂停。我不这么认为,但是如果你能够将视频存储在url以.mp4或有效视频文件名结尾的任何服务上,那么你就可以播放它了。请记住,这些服务不是