Search Unity

Video Video and Audio from StreamingAssets folder

Discussion in 'Audio & Video' started by LabMatti, Mar 21, 2018.

  1. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    55
    Hi all!
    I need to load a video (mp4) from the StreamingAssets folder, and to play it.
    The video works without problems, but the audio doesn't.

    I'm trying with this code:

    public string url;
    public AudioSource source;
    private AudioSource audiosrc;
    public VideoPlayer video;

    // Use this for initialization
    IEnumerator Start()
    {
    url = "File://" + Application.dataPath + "/StreamingAssets/" + 1 + ".mp4";
    video = GetComponent<VideoPlayer> ();
    using (var www = new WWW(url))
    {
    yield return www;
    audiosrc = Instantiate (source);
    audiosrc.clip = www.GetAudioClip ();
    video.url = url;
    video.controlledAudioTrackCount = 1;
    video.EnableAudioTrack (0, true);
    video.SetTargetAudioSource (0, audiosrc.GetComponent<AudioSource>());
    video.Play ();
    }
    }

    The error is:
    Unable to determine the audio type from the URL (File://D:/.../simpleVideoPlayer/Assets/StreamingAssets/1.mp4) . Please specify the type.

    Is it even possible to play a video with its audio from streamingAssets?

    Thank you!
     
  2. Mitnainartinarian

    Mitnainartinarian

    Joined:
    Jun 3, 2017
    Posts:
    16
    You shouldn't have to set the audioSource.clip, and make sure video.audioOutputMode is set to VideoAudioOutputMode.AudioSource. Here's the code that I use and it works fine (I'm using an mp4 as well):


    public void PlayStreamingClip(string videoFile, bool looping = true) {
    videoPlayer.source = VideoSource.Url;
    videoPlayer.url = Application.streamingAssetsPath + "/" + videoFile;
    videoPlayer.isLooping = looping;
    StartCoroutine(PlayVideo());
    }

    private IEnumerator PlayVideo() {
    // We must set the audio before calling Prepare, otherwise it won't play the audio
    var audioSource = videoPlayer.GetComponent<AudioSource>();
    videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    videoPlayer.controlledAudioTrackCount = 1;
    videoPlayer.EnableAudioTrack(0, true);
    videoPlayer.SetTargetAudioSource(0, audioSource);

    // Wait until ready
    videoPlayer.Prepare();
    while (!videoPlayer.isPrepared)
    yield return null;

    videoPlayer.Play();
    videoImage.texture = videoPlayer.texture;

    while (videoPlayer.isPlaying)
    yield return null;

    onVideoFinished.Invoke(this);
    }
     
  3. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    55
    Thank you! Tomorrow I will try it!
     
  4. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    55
    HI, sorry if I write back to you so late but I had to abbandon what I was doing.

    I don't understand how to use your script..what is "videoImage"?

    I don't understand why this is not automatic...
     
  5. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    55
    Ok now I do understand! But it remain strange to me. Ok I just created a RawImage and I hidden it, now the sound works!
     
  6. navya_unity

    navya_unity

    Joined:
    Jul 23, 2018
    Posts:
    1
    Hi!!
    I am getting an error which says - Can't play movie [path of video] when I use this code though the video is available in the given path.
     
  7. LabMatti

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    55
    Hi, is the video format supported? try to convert the video in mp4 (h.264)
     
  8. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    10
    Hey, this is closely related to this topic but I can't seem to get a straight answer. Would there be a way to load just an AudioClip from the streaming assets folder into a gameObject's AudioSource for example?