LoginSignup
1
0

More than 1 year has passed since last update.

Unity:S3から複数の動画を選択しストリーミング再生する方法

Posted at

S3に置いた動画データを選択して配信する

S3に置いた複数の動画データから配信する動画を選択する方法についてまとめます。

実装したもの

ボタンで選択することで流れる動画が切り替わるように実装しました。

S3のバケットに動画を置く

まずはS3のバケット内に動画を置きます。
Try #078 – Amazon S3の公開設定を色々とためしてみた
の記事を参考にして、公開されたURLを取得します。

Script

VideoDownloader.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class VideoDownloader : MonoBehaviour
{
    bool canPlay = false;
    VideoPlayer video;
    string[] videoURL = new string[5];
    
    void Start()
    {
        VideoPlay();

    }

    public void VideoPlay()
    {
        video = gameObject.GetComponent<VideoPlayer>();
        video.playOnAwake = false;
        video.waitForFirstFrame = true;
        video.source = VideoSource.Url;
        videoURL[0] = "https://バケット名.s3.リージョン.amazonaws.com/ファイル名/動画ファイル名";
        videoURL[1] = "https://バケット名.s3.リージョン.amazonaws.com/ファイル名/動画ファイル名";
        video.url = videoURL[0];
        
        video.prepareCompleted += VideoPlayerOnPrepareCompleted;
        video.Prepare();
    }

    private void VideoPlayerOnPrepareCompleted(VideoPlayer source)
    {
        canPlay = true;
    }

    public void OnClickVideoPlay0()
    {
        if(canPlay == true)
        {
            video.url = videoURL[0];
            video.Play();
            Debug.Log("0");
        }
        
    }

    public void OnClickVideoPlay1()
    {
        if (canPlay == true)
        {
            video.url = videoURL[1];
            video.Play();
            Debug.Log("1");
        }

    }
}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0