- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
MovieTexture在 Unity 5.6.0b1 发布后最终被弃用,现在发布了在桌面和移动设备上播放视频的新 API。
VideoPlayer和 VideoClip可用于播放视频并在需要时检索每一帧的纹理。
我已经设法让视频正常工作,但无法让音频在 Windows 10 上的编辑器中播放。有人知道为什么音频不播放吗?
//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");
}
最佳答案
发现问题。下面是播放视频和音频的FIXED代码:
//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");
}
为什么没有播放音频:
//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);
必须在 videoPlayer.Prepare();
之前调用,而不是在它之后。这花了几个小时的实验才发现这是我遇到的问题。
卡在“准备视频”?
在调用 videoPlayer.Prepare();
后等待 5 秒,然后退出 while 循环。
替换:
while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
yield return null;
}
与:
//Wait until video is prepared
WaitForSeconds waitTime = new WaitForSeconds(5);
while (!videoPlayer.isPrepared)
{
Debug.Log("Preparing Video");
//Prepare/Wait for 5 sceonds only
yield return waitTime;
//Break out of the while loop after 5 seconds wait
break;
}
这应该可行,但您可能会在视频开始播放时遇到缓冲。在使用此临时修复程序时,我的建议是提交标题为“videoPlayer.isPrepared always true”的错误,因为这是一个错误。
一些 people还通过更改修复了它:
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
到
videoPlayer.playOnAwake = true;
audioSource.playOnAwake = true;
从 URL 播放视频:
替换:
//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;
与:
//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";
然后删除:
public VideoClip videoToPlay;
和 videoPlayer.clip = videoToPlay;
因为它们不再需要了。
播放 StreamingAssets 文件夹中的视频:
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;
所有支持的视频格式:
Windows 上额外支持的视频格式:
其中一些格式在某些平台上不起作用。参见 this发布有关支持的视频格式的更多信息。
关于c# - 使用新的 Unity VideoPlayer 和 VideoClip API 播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41144054/
我正在尝试在基本视频的基础上制作大约 6 到 7 个 gif 动画剪辑的集合。这些动画图标显示在视频中心的半圆上。我正在寻找编写此逻辑的最佳方法,但在此停留了一段时间。任何帮助深表感谢。 positi
我写了下面的代码: from moviepy.editor import * from PIL import Image clip= VideoFileClip("video.mp4") video=
MovieTexture在 Unity 5.6.0b1 发布后最终被弃用,现在发布了在桌面和移动设备上播放视频的新 API。 VideoPlayer和 VideoClip可用于播放视频并在需要时检索每
MovieTexture在 Unity 5.6.0b1 发布后最终被弃用,现在发布了在桌面和移动设备上播放视频的新 API。 VideoPlayer和 VideoClip可用于播放视频并在需要时检索每
我是一名优秀的程序员,十分优秀!