solve the problem of audio playback in the UNITY VideoPlayer on Windows 10
Unity's VideoPlayer and VideoClip API provides a powerful way to play videos in the project. However, some users have encountered audio playback problems on the desktop platform.
Problem description:
A unity developer cannot play audio when using VideoPlayer API on Windows 10, even if the video can be displayed normally.
Solution:
After investigation, it is found that the correct order of audio attributes is essential for successful audio playback. The following modification of the code fragment provided solved this problem:
By setting the audio output mode and audio track attributes before calling Prepare (), the audio playback is now enabled.Other tips:
<code class="language-c#">// 在准备视频之前将音频输出设置为AudioSource videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource; // 启用音频轨道并设置目标AudioSource videoPlayer.EnableAudioTrack(0, true); videoPlayer.SetTargetAudioSource(0, audioSource); // 准备视频 videoPlayer.Prepare();</code>
If the "being preparing video" log message is retained indefinitely, please consider adding timeout to the While to prevent the script from stagnation:
<code class="language-c#">WaitForSeconds waitTime = new WaitForSeconds(5); while (!videoPlayer.isPrepared) { Debug.Log("正在准备视频"); yield return waitTime; break; }</code>
The above is the detailed content of Why Is My Unity VideoPlayer Not Playing Audio on Windows 10?. For more information, please follow other related articles on the PHP Chinese website!