web-dev-qa-db-ja.com

新しいUnity VideoPlayerおよびVideoClip APIを使用してビデオを再生する

MovieTexture は、Unity 5.6.0b1のリリースおよびデスクトップとモバイルデバイスの両方でビデオを再生する新しいAPIのリリース後に廃止されました。

VideoPlayer および VideoClip を使用して、ビデオを再生し、必要に応じて各フレームのテクスチャを取得できます。

ビデオを機能させることはできましたが、Windows 10のエディターから音声を再生することはできませんでした。音声が再生されない理由は誰にもわかりませんか?

//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");
}
35
Programmer

問題が見つかりました。以下は、ビデオとオーディオを再生するFIXEDコードです。

//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 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);

    //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");

    //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");
}

オーディオが再生されなかった理由:

//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);

videoPlayer.Prepare();の後ではなく前に呼び出す必要があります。これは、これが私が抱えていた問題であることがわかるまでに何時間もの実験を要しました。


「ビデオの準備」で止まっていますか?

videoPlayer.Prepare();が呼び出されてから5秒待ってから、whileループを終了します。

交換:

while (!videoPlayer.isPrepared)
{
    Debug.Log("Preparing Video");
    yield return null;
}

で:

//Wait until video is prepared
WaitForSeconds waitTime = new WaitForSeconds(5);
while (!videoPlayer.isPrepared)
{
    Debug.Log("Preparing Video");
    //Prepare/Wait for 5 sceonds only
    yield return waitTime;
    //Break out of the while loop after 5 seconds wait
    break;
}

これは機能するはずですが、ビデオの再生を開始するとバッファリングが発生する場合があります。この一時的な修正を使用している間、これはバグであるため、「videoPlayer.isPrepared always true」というタイトルでバグを報告することをお勧めします。

一部の people も変更して修正しました:

videoPlayer.playOnAwake = false; 
audioSource.playOnAwake = false;

videoPlayer.playOnAwake = true; 
audioSource.playOnAwake = true;

URLからビデオを再生:

交換:

//We want to play from video clip not from url
videoPlayer.source = VideoSource.VideoClip;

with:

//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

次に削除

public VideoClip videoToPlay;videoPlayer.clip = videoToPlay;はもう必要ないためです。

StreamingAssetsフォルダーからビデオを再生:

string url = "file://" + Application.streamingAssetsPath + "/" + "VideoName.mp4";

if !UNITY_EDITOR && UNITY_Android
    url = Application.streamingAssetsPath + "/" + "VideoName.mp4";
#endif

//We want to play from url
videoPlayer.source = VideoSource.Url;
videoPlayer.url = url;

サポートされているすべてのビデオ形式

  • ogv
  • vp8
  • webm
  • mov
  • dv
  • mp4
  • m4v
  • mpg
  • mpeg

Windowsでサポートされる追加のビデオ形式

  • avi
  • asf
  • wmf

これらの形式の一部は、一部のプラットフォームでは機能しません。サポートされているビデオ形式の詳細については、 this postを参照してください。

56
Programmer

他の答えが言っていることに似ています。ビデオの準備と終了の状態にコールバックを使用できます。コルーチンを使用してリターンを生み出すのではなく。

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

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

void EndReached(VideoPlayer vp) {
    // do something
}
8
Groshh

@Programmerの回答を使用してURLからビデオを再生しましたが、再生するサウンドを取得できませんでした。最終的に、YouTubeチュートリアルのコメント で答えを見つけました。

URL経由で読み込まれた映画の音声を再生するには、EnableAudioTrackを呼び出す前に次の行を追加する必要があります。

videoPlayer.controlledAudioTrackCount = 1;
1
TarkaDaal