Uso del nuevo Unity VideoPlayer y VideoClip API para reproducir videos

Resuelto Programmer asked hace 7 años • 4 respuestas

MovieTexture finalmente queda obsoleto después del lanzamiento de Unity 5.6.0b1 y ahora se lanza una nueva API que reproduce videos tanto en dispositivos móviles como de escritorio.

VideoPlayer y VideoClip se pueden utilizar para reproducir vídeo y recuperar textura para cada fotograma si es necesario.

Logré que el video funcionara, pero no pude reproducir el audio también desde el Editor en Windows 10. ¿Alguien sabe por qué no se reproduce el audio?

//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");
}
Programmer avatar Dec 14 '16 20:12 Programmer
Aceptado

Similar a lo que han estado diciendo las otras respuestas. Puede utilizar devoluciones de llamada para los estados de preparación y finalización del vídeo. En lugar de utilizar corrutinas y rendimiento.

videoPlayer.loopPointReached += EndReached;
videoPlayer.prepareCompleted += PrepareCompleted;

void PrepareCompleted(VideoPlayer vp) {
    vp.Play();
}

void EndReached(VideoPlayer vp) {
    // do something
}
Groshh avatar Apr 27 '2017 22:04 Groshh

Utilicé la respuesta de @Programmer para reproducir vídeos desde una URL, pero no pude reproducir ningún sonido. Finalmente encontré la respuesta en los comentarios de un tutorial de YouTube.

Para reproducir el audio de una película cargada a través de URL, debe agregar la siguiente línea antes de la llamada a EnableAudioTrack:

videoPlayer.controlledAudioTrackCount = 1;
TarkaDaal avatar Jul 10 '2018 13:07 TarkaDaal