在Unity的Google VR Cardboard中将视频流式传输到Android设备

时间:2017-06-07 19:53:24

标签: android unity3d virtual-reality

我一直在尝试为Android上的Google Cardboard制作一个VR视频播放器应用程序,可以在其中流式传输或下载视频。视频在编辑器中工作正常,但在我的手机中不起作用。我在Windows 10上使用Unity 5.6.1f1,手机是在Noughat上运行的Moto G4 plus。

以下是用于流式传输或下载视频的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GazeButton : MonoBehaviour {

public static bool toStream;
//private bool buttonPress;
public Image streamImg;
public Image downImg;
public Slider progress;
public Text txt;
//public WWW url;
//public GvrVideoPlayerTexture
private PlayVideo thePlayVideo;
public Camera theCamera;


// Use this for initialization
void Start () {

    if (theCamera == null)
        theCamera = GameObject.Find ("Main Camera").GetComponent<Camera> ();

    if(streamImg == null)
        streamImg = GameObject.Find ("StreamImage").GetComponent<Image>();

    if(downImg == null)
        downImg = GameObject.Find ("DownImage").GetComponent<Image>();

    streamImg.color = Color.green;
    downImg.color = Color.red;

    if (progress == null)
        progress = GameObject.Find ("ProgressSlider").GetComponent<Slider> ();

    progress.value = 0;
    progress.gameObject.SetActive (false);

    if (txt == null)
        txt = GameObject.Find ("GuideText").GetComponent<Text>();

    thePlayVideo = FindObjectOfType<PlayVideo> ();
}

// Update is called once per frame
void Update () {
    if (progress.IsActive()) {
        progress.value += 1;

        if (progress.value >= progress.maxValue /*&& buttonPress*/) {
            if (toStream) {
                streamImg.color = Color.gray;
                streamImg.gameObject.SetActive (false);
                downImg.gameObject.SetActive (false);
                progress.gameObject.SetActive (false);
                txt.gameObject.SetActive (false);
                //FlipCameraView ();
                thePlayVideo.Stream ();
            } else {
                downImg.color = Color.gray;
                streamImg.gameObject.SetActive (false);
                downImg.gameObject.SetActive (false);
                progress.gameObject.SetActive (false);
                txt.gameObject.SetActive (false);
                //FlipCameraView ();
                thePlayVideo.Download ();
            }

        }
    }
}

public void StreamButtonDown(){
    streamImg.color = Color.blue;
    toStream = true;
    //buttonPress = true;
    progress.gameObject.SetActive (true);
    progress.value = 0;
}

public void DownButtonDown(){
    downImg.color = Color.blue;
    toStream = false;
    //buttonPress = true;
    progress.gameObject.SetActive (true);
    progress.value = 0;
}

public void StreamButtonUp(){
    streamImg.color = Color.green;
    //buttonPress = false;
    progress.gameObject.SetActive (false);
}

public void DownButtonUp(){
    downImg.color = Color.red;
    //buttonPress = false;
    progress.gameObject.SetActive (false);
}

public bool GetCondition(){
    return toStream;
}

}

这用于实际流式传输视频:

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

public class PlayVideo : MonoBehaviour {

//public GameObject theSphere;
private VideoPlayer theVideoPlayer;
//private VideoSource theVideoSource;

private AudioSource theAudioSource;
public GazeButton theGazeButton;

// Use this for initialization
void Start () {
    /*if (theSphere == null)
        theSphere = GameObject.Find ("Sphere");*/
    theGazeButton = GetComponent<GazeButton> ();
}

// Update is called once per frame
void Update () {
    if (theVideoPlayer != null) {
        if (/*(!theGazeButton.GetCondition ()) &&*/ theVideoPlayer.isPrepared) {
            theVideoPlayer.Play ();
            theAudioSource.Play ();
        } 
    }
}

public void RealStart(){
    theVideoPlayer = gameObject.AddComponent<VideoPlayer> ();
    //theVideoSource = gameObject.AddComponent<VideoSource> ();
    theAudioSource = gameObject.AddComponent<AudioSource> ();

    theVideoPlayer.source = VideoSource.Url;
    theVideoPlayer.url = "https://<SOME LINK>.mp4";
    //theSphere.AddComponent<VideoPlayer>(theVideoPlayer);

    theVideoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    theVideoPlayer.EnableAudioTrack (0, true);
    theVideoPlayer.SetTargetAudioSource (0, theAudioSource);
}

public void Stream(){
    RealStart ();
    theVideoPlayer.playOnAwake = true;
    theVideoPlayer.Play ();
    theAudioSource.Play ();
}

public void Download(){
    RealStart ();
    theVideoPlayer.playOnAwake = false;
    theVideoPlayer.Prepare ();

}
}

我不能为我的生活,理解为什么视频在编辑器中完美运行而不是在手机上运行。请帮忙

1 个答案:

答案 0 :(得分:0)

使用现代的Unity编辑器(至少2017.4 LTS)并安装最新的Google VR SDK for Unity。在此处,查看 VideoDemo.scene 文件以在Unity上的Google Cardboard中观看视频。