Video playback cuts off the last few seconds - unity3d

I'm playing a video in unity, and short clips are fine, but longer ones cut off the last few seconds. My video is 52 seconds and stops when about 50 have played. I tried a random 2:45 video from the net, and it stopped after 2:41. I can't figure out what's happening.
I tried video clip and URL. Both have the issue.
IEnumerator playVideo(SubjectClipState clipState)
{
clipState.clipObject.videoPlayer.Stop();
clipState.clipObject.audioSource.Stop();
clipState.clipObject.videoPlayer.playOnAwake = false;
clipState.clipObject.audioSource.playOnAwake = false;
clipState.clipObject.videoPlayer.source = VideoSource.Url;
clipState.clipObject.videoPlayer.audioOutputMode = clipState.clipConfig.hasAudio ? VideoAudioOutputMode.AudioSource : VideoAudioOutputMode.None;
clipState.clipObject.videoPlayer.controlledAudioTrackCount = clipState.clipConfig.hasAudio ? Convert.ToUInt16(1) : Convert.ToUInt16(0);
clipState.clipObject.videoPlayer.EnableAudioTrack(0, clipState.clipConfig.hasAudio);
if (clipState.clipConfig.hasAudio) clipState.clipObject.videoPlayer.SetTargetAudioSource(0, clipState.clipObject.audioSource);
clipState.clipObject.videoPlayer.url = clipState.clipConfig.url;
clipState.clipObject.videoPlayer.isLooping = clipState.clipConfig.endAction.type == "loop";
clipState.clipObject.videoPlayer.Prepare();
while (!clipState.clipObject.videoPlayer.isPrepared)
{
yield return null;
}
clipState.clipObject.videoPlayer.Play();
}
Note: the videoPlayer and audioSource are set elsewhere in the code. LoopPointReached is attached.
I am playing it like this:
StartCoroutine("playVideo", subjectClipState);
LoopPointReached will break a couple seconds before it should be done. The video player x/x frames is correct. e.g. 2740/2740 frames.

ok, it turns out it was a bug with Unity 2018.2. When I upgraded to 2018.3, it didnt cut off.

Related

White frame before video plays in Unity instead of a custom thumbnail

I load a thumbnail before the video starts to play, but later when the video is playing, there is first a white frame and then the video is playing. How can I avoid this white frame??
Here is my code-
video.GetComponent<RawImage>().texture = thumbnailTex;
//Play the video:
RenderTexture rt = new RenderTexture(1920, 1080, 16, RenderTextureFormat.ARGB32);
rt.Create();
video.GetComponent<RawImage>().texture =rt;
video.GetComponent<RawImage>().targetTexture=rt;
video.GetComponent<VideoPlayer>().url = "www....";
video.GetComponent<VideoPlayer>().Play();
// white frame, and then the video is playing
You need to wait first and test if the video is ready to play
It would be better if it's not already to have the above code in a coroutine. What is happening is you call play before the player has had a chance to download/load the first frame. Then display your rendertexture.
video.GetComponent().url = "...";
video.GetComponent().Prepare();
while (!video.GetComponent().isPrepared)
yield return new WaitForEndOfFrame();
video.GetComponent().frame = 0; //just incase it's not at the first frame
video.GetComponent().Play();
//now display your render texture
Thanks you very much for this solution to avoid jump frames during the playing of a video!
But there are some changes to do :
In the "Start" function, I call a coroutine :
StartCoroutine(PrepareVideoCoroutine());
In this coroutine, I put :
while (!gameOverGlassVideo.GetComponent().isPrepared) {
gameOverGlassVideo.GetComponent<VideoPlayer>().Prepare();
yield return new WaitForEndOfFrame();
}
//::: I put the line to prepare the video in the condition and NOT
outside ::://
When I want to play the video after, it's was already prepared and there will be not jumping frames!

Flutter - Stop audio playback from recorded video in the background

Essentially the app is like snapchat. I take pics and reset back to camera mode, the issue comes when I record video and reset, it goes back to camera mode but the audio form the video keeps playing in the background. The functions are somwhat exactly like the camera doc, with a few addition to reset the camera.
I added this:
_reset() {
if (mounted)
setState(() {
if (this._didCapture) {
this._didCapture = false;
this._isRecording = false;
this._isPosting = false;
this._file = File('');
this._fileType = null;
this._captions.clear();
this._textEditingControllers.clear();
this._videoController = null;
this._videoPlayerListener = null;
}
});
}
It works just fine but the audio in the background is still on. Also wondering if the video/picture is saved on the phone, which I don't want to...
i had been looking for a similar answer, but i didn´t find it. You could try to stop it adding this to your function:
this._controller.setVolume(0.0);
that´s what i did in my app

Animation Trigger set in IEnumerator causes Animation to get stuck

private IEnumerator reviveCountdown() {
timeLeft = 5;
while (timeLeft >= 0) {
reviveAnim.ResetTrigger ("AdReviveTrigger");
reviveAnim.SetTrigger ("AdReviveTrigger");
reviveCountdownText.text = timeLeft.ToString();
yield return new WaitForSecondsRealtime(1.0f);
timeLeft--;
}
}
I'm using the above IEnumerator to show a timer that counts down from 5 to 0, and animates the text each time it counts down. The countdown works correctly. The problem I'm having is the animation IS TRIGGERED but never actually plays because it gets stuck on the animation state, as shown in the screenshot below. It sits in the AdRevive state for the entire 5 seconds but never plays. The transition from Idle is a simple trigger set in the code. The animation works if I play it manually in the Unity editor. Anyone know why it gets stuck?
I solved my own issue. The Time.timeScale was set to 0 because the game was paused during this animation, so it would never play. To fix, I set the animationUpdateMode to UnscaledTime.

Single Audiosource for multiple clips?

I have a couple of audio sources in a script, idea being that one is used for effects and the other for music. I am noticing that if an effect plays at the same time as another one, it actually stops the previous one from playing, probably because they are playing from the same audio source.
My question is, do I really have to instantiate a new audio source every time i want to play a new sound, just to make sure that one sound will not stop a currently playing one ? Is there a better way ?
The method that plays a sound effect(and the awake one) in my class are these :
void Awake() {
audioSource = GetComponents<AudioSource>()[0];
audioEffectsSource = GetComponents<AudioSource>()[1];
audioSource.volume = PlayerPrefsManager.getMasterVolume();
audioEffectsSource.volume = PlayerPrefsManager.getSFXMasterVolume();
}
public void playSoundEffect(AudioClip clip)
{
if(clip != null)
{
audioEffectsSource.clip = clip;
audioEffectsSource.volume = PlayerPrefsManager.getSFXMasterVolume();
audioEffectsSource.PlayOneShot(clip);
}
}
The script is attached to a musicmanager gameobject with 2 audiosources, one for music fx and one for sound tracks.

MovieTexture won't play audio

I'm trying to dynamically load and play a video file. No matter what I do, I cannot seem to figure out why the audio does not play.
var www = new WWW("http://unity3d.com/files/docs/sample.ogg");
var movieTexture = www.movie;
var movieAudio = www.movie.audioClip;
while (!movieTexture.isReadyToPlay) yield return 0;
// Assign movie texture and audio
var videoAnimation = videoAnimationPrefab.GetComponent<VideoAnimation>();
var videoRenderer = videoAnimation.GetVideoRenderer();
var audioSource = videoAnimation.GetAudioSource();
videoRenderer.material.mainTexture = movieTexture;
audioSource.clip = movieAudio;
// Play the movie and sound
movieTexture.Play();
audioSource.Play();
// Double check audio is playing...
Debug.Log("Audio playing: " + audioSource.isPlaying);
Every time I receive Audio playing: False
I've also tried using a GUITexture using this as a guide, but no dice. There are no errors displayed in the console.
What am I doing wrong that makes the audio never work?
Thanks in advance for any help!
Changed to:
while (!movieTexture.isReadyToPlay) yield return 0;
var movieAudio = movieTexture.audioClip;
Even though AudioClip inherits from Object, a call to movieTexture.audioClip seems to return a copied version instead of returning a reference by value to the object. So at the time I was assigning it, it had not been created yet and had to wait until the movie was "Ready to Play" until fetching the audioClip.