Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    58
    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);
    }
     
    Amb_Verd likes this.
  3. LabMatti

    LabMatti

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

    LabMatti

    Joined:
    Jan 19, 2018
    Posts:
    58
    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:
    58
    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:
    58
    Hi, is the video format supported? try to convert the video in mp4 (h.264)
     
  8. EthanHooper1023

    EthanHooper1023

    Joined:
    Jan 19, 2017
    Posts:
    24
    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?
     
  9. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858

    Code (CSharp):
    1. UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip( filePath, AudioType.WAV );
    2. yield return request.SendWebRequest();
    3.  
    4. if( request.isNetworkError ) {
    5.     Debug.LogWarning( request.error + "\n" + filePath );
    6. } else {
    7.     AudioClip clip = DownloadHandlerAudioClip.GetContent( request );
    8.     clip.name = fileName;
    9.     // Assign to audio source here.
    10. }
     
    swingingtom and RealfictionLab like this.