gpt4 book ai didi

c# - unity 为什么我的视频播放器上没有播放音频?

转载 作者:行者123 更新时间:2023-12-02 22:25:44 25 4
gpt4 key购买 nike

好的,我以前从未统一使用过视频播放器,设法让它在 android 和编辑器上运行,但音频不会播放,我从 url 获取视频并将视频播放器指向连接到同一对象的音频源视频播放器组件已附加,我遵循此链接的建议 Using new Unity VideoPlayer and VideoClip API to play video但还没有任何快乐,但下面是我如何称呼它的顺序,如提到的视频作品,但音频没有,谁能帮忙

 private AudioSource audio;
private VideoPlayer screen;

screen = go.GetComponentInChildren<VideoPlayer>();
audio = go.GetComponentInChildren<AudioSource>();
audio.playOnAwake = false;
screen.source = VideoSource.Url;
screen.url = videoObj.VideoURL;
screen.audioOutputMode = VideoAudioOutputMode.AudioSource;
screen.EnableAudioTrack (0,true);
screen.SetTargetAudioSource (0,audio);
screen.Prepare ();
screen.Play();
audio.Play ();

编辑

根据下面的评论,我添加了一些信息并澄清了一些观点,所以统一版本是 2017 音频不会在编辑器或任何设备上播放,但视频在编辑器和设备上工作正常,这是一个 url我正在尝试 https://storage.googleapis.com/godapp-a3e4b.appspot.com/jacopatrolmontage.mp4这是完整的代码,或者我正在使用的方法请不要拆开其他我可能做错的东西,因为我对统一和 C# 相当陌生
public void showPlayer(VideoObjects videoObj)
{
if (GameObject.Find("videoPlaner(Clone)") == null)
{
//may need scene.isValid
GameObject go = Instantiate(videoPlayer);
go.transform.SetParent(canvas.transform, false);
screen = go.GetComponentInChildren<VideoPlayer>();
audio = go.GetComponentInChildren<AudioSource>();
//audio = go.AddComponent<AudioSource>();
fsscreenBool = true;
screenSize = go.GetComponent<Transform> ().localScale;
screenPosition = go.GetComponent<Transform> ().localPosition;
canvasRectTransformScale = go.GetComponentInParent<RectTransform>
();
foreach (Transform tr in go.transform)
{
if (tr.gameObject.tag == "play")
{
Button play = tr.GetComponent<Button>();
AddPlayListener(play, screen);
preparePlayer(play,screen.isPlaying);
}
if (tr.gameObject.tag == "fullscreen")
{
Button fullScreen = tr.GetComponent<Button>();
AddFSListener(fullScreen, go);
}
if (tr.gameObject.tag == "forward")
{
}
if (tr.gameObject.tag == "rewind")
{
}
if (tr.gameObject.tag == "vidtitle")
{
tr.GetComponent<Text>().text = videoObj.VideoTitle;
}
if (tr.gameObject.tag == "loading")
{
loading = tr.gameObject;
}
}
}
else{
//print("it exists");
GameObject existingGO = GameObject.Find("videoPlaner(Clone)");
screen = existingGO.GetComponentInChildren<VideoPlayer>();
loading.SetActive (true);
foreach (Transform tr in existingGO.transform)
{
if (tr.gameObject.tag == "play")
{
}
if (tr.gameObject.tag == "fullscreen")
{
checkScreen (existingGO);
}
if (tr.gameObject.tag == "forward")
{
}
if (tr.gameObject.tag == "rewind")
{
}
if (tr.gameObject.tag == "vidtitle")
{
tr.GetComponent<Text>().text = videoObj.VideoTitle;
}
if (tr.gameObject.tag == "loading")
{
}
}
}
screen.source = VideoSource.Url;
screen.url = videoObj.VideoURL;
screen.audioOutputMode = VideoAudioOutputMode.AudioSource;
screen.EnableAudioTrack (0,true);
screen.SetTargetAudioSource (0,audio);
screen.Prepare ();
audio.Play ();
screen.Play();
}

最佳答案

您调用screen.Prepare ()而不是等它准备好再打电话screen.Play()audio.Play () .您还设置了audio.playOnAwake = false;但没有对视频这样做。您还应该在 playOnAwake 上将视频设为假。 .在播放之前给视频时间加载。

screen.Prepare();

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

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

//Play Video
screen.Play();

//Play Sound
audio.Play();

您可能希望将您的音频变量重命名为其他名称,因为这是一个已被声明的已弃用变量 MonoBehaviour .最后,使用 answer 中的代码在您链接的问题中。那应该可以用 RawImage 播放视频.

it still works in fact with the raw image i have a duplicate video



有一次发生在我身上,然后我意识到 Unity 寻找 Renderer在具有任何 VideoPlayer 的 GameObject 上附加到它的任何脚本中的代码然后自动更新 Texture每一帧。我认为他们这样做是为了简化 VideoPlayer 的使用。 .

您应该将该脚本附加到没有 Renderer 的空游戏对象上。然后你可以继续把 RawImage在编辑器中的图像插槽上。不会有重复的。

关于c# - unity 为什么我的视频播放器上没有播放音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46505665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com