MBS
MBS

Reputation: 707

Unity: The audio of a video file is choppy when played with Unity

I face a problem when playing some videos on unity editor or unity application (windows standalone application). The audio of a video file is choppy.

If I play the recorded video file on Windows using any player the problem will not appear.

After working on this issue I think I figured out the problem: The duration of the audio of the recorded video is larger than the duration of the video it self.

You can see the details of a normal video and recorded video.

Normal video details: https://drive.google.com/file/d/1qzKSr-Fb2ohb2W1xwooZum0xT3acQZM4/view?usp=sharing

Recorded video details: https://drive.google.com/file/d/1jpr2IJwtIByLsWh7itVfm0nhoprarNm4/view?usp=sharing

My question is with 2 parts:

1- Can the difference in durations make this problem.

2- How to fix this problem in unity?

private IEnumerator PlayVideo(string filePath)
{
    //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;
    _audioSource.Pause();

    //We want to play from video clip not from url

    //videoPlayer.source = VideoSource.VideoClip;

    // Vide clip from Url
    _videoPlayer.source = VideoSource.Url;
    _videoPlayer.url = filePath;


    //Set Audio Output to AudioSource
    _videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

    //Assign the Audio from Video to AudioSource to be played
    _videoPlayer.controlledAudioTrackCount = 1;
    _videoPlayer.EnableAudioTrack(0, true);
    _videoPlayer.SetTargetAudioSource(0, _audioSource);

    //Set video To Play then prepare Audio to prevent Buffering
    //videoPlayer.clip = myVideoClip;
    _videoPlayer.Prepare();

    //Wait until video is prepared
    WaitForSeconds waitTime = new WaitForSeconds(1);
    _retryCount = 0;
    while (!_videoPlayer.isPrepared)
    {
        Debug.Log("Preparing Video");
        //Prepare/Wait for 15 sceonds only
        yield return waitTime;
        _retryCount++;
        if (_retryCount >= _maximumRetries)
        {
            break;
        }
    }

    _vidFrameLength = (int)_videoPlayer.frameCount;
    ProgressBar.maxValue = _vidFrameLength;

    Debug.Log("Done Preparing Video");
    VideoPlayerImage.texture = _videoPlayer.texture;

    _videoPlayer.Play();
    _audioSource.Play();
    ClipLength.text = StringHelper.ToTime((float)_videoPlayer.frameCount / _videoPlayer.frameRate, TimePreviewer.Minutes);

    while (_videoPlayer.isPlaying)
    {
        yield return null;
    }
}

Upvotes: 1

Views: 5124

Answers (3)

MBS
MBS

Reputation: 707

For now, I found 2 video player libraries on Unity store that plays the choppy video without problems.

I am using one of them until Unity fixes the problem regarding playing variable frame rate videos.

https://assetstore.unity.com/packages/tools/video/ump-win-mac-linux-webgl-49625 https://assetstore.unity.com/packages/tools/video/avpro-video-windows-57969

I know this is not the solution for the question, but until anyone gives a good answer to the question I will use this.

Upvotes: 0

Flame Cheer
Flame Cheer

Reputation: 1

Try to modify the audiousource load type to "steaming" at the inspector.

Upvotes: -1

buffalo94
buffalo94

Reputation: 159

Assuming you are using VideoPlayer component try to change Audio Output Mode from AudioSource to Direct vise versa to see if that's change something.

Upvotes: 0

Related Questions