Search Unity

Question video played via script not working in build

Discussion in 'Audio & Video' started by shampaw, Dec 3, 2023.

  1. shampaw

    shampaw

    Joined:
    Dec 3, 2023
    Posts:
    2
    i have this script on 2nd scene that plays an entrance video and then loops idle video, on the first scene i have a video playing on background, the video is inserted directly in a video player.

    on play mode, the video on 2nd scene plays fine, but when i build it, the videos on 2nd scene dont work

    i tried using singleton pattern but nothing changes, i also tried playing the video on 1st scene through a script but the same thing happen

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using TMPro;
    7. using UnityEngine.Video;
    8. using UnityEngine.SceneManagement;
    9.  
    10. public class VideoPlayerController : MonoBehaviour
    11. {
    12.     public VideoPlayer videoPlayer;
    13.  
    14.     void Start()
    15.     {
    16.         // Get the VideoPlayer component attached to this GameObject
    17.         videoPlayer = GetComponent<VideoPlayer>();
    18.  
    19.         // Check if the VideoPlayer component is found
    20.         if (videoPlayer != null)
    21.         {
    22.             // Optionally, set up event listeners for video playback events
    23.             videoPlayer.loopPointReached += OnVideoEnd;
    24.  
    25.             // Play the video based on the stored combined names
    26.             PlayVideo();
    27.         }
    28.         else
    29.         {
    30.             Debug.LogError("VideoPlayer component not found!");
    31.         }
    32.     }
    33.  
    34.     public void PlayVideo()
    35.     {
    36.         // Retrieve the combined names from PlayerPrefs
    37.         string storedcombinedNames = PlayerPrefs.GetString("CombinedNames", "");
    38.  
    39.         if (!string.IsNullOrEmpty(storedcombinedNames))
    40.         {
    41.             // Assuming your videos are in the "Assets/Videos/" directory
    42.             string videoFilename = storedcombinedNames + ".mp4";
    43.  
    44.             // Set the video URL and play the video
    45.             videoPlayer.url = "Assets/Videos/ENTRANCE/" + videoFilename;
    46.             videoPlayer.Play();
    47.         }
    48.         else
    49.         {
    50.             Debug.LogError("CombinedNames not found in PlayerPrefs!");
    51.         }
    52.     }
    53.  
    54.     public void RearrangeAndStoreNames()
    55.     {
    56.         videoPlayer.Stop();
    57.         string storedCombinedNames = PlayerPrefs.GetString("CombinedNames", "");
    58.  
    59.         if (!string.IsNullOrEmpty(storedCombinedNames))
    60.         {
    61.             // Split the names into an array
    62.             string[] namesArray = storedCombinedNames.Split(", ");
    63.  
    64.             // Rearrange the array (e.g., switch elements at index 0 and 2)
    65.             if (namesArray.Length >= 4)
    66.             {
    67.                 string temp = namesArray[0];
    68.                 namesArray[0] = namesArray[2];
    69.                 namesArray[2] = temp;
    70.  
    71.                 // Join the rearranged array back into a string
    72.                 string rearrangedNames = string.Join(", ", namesArray);
    73.  
    74.                 // Store the rearranged names back to PlayerPrefs
    75.                 PlayerPrefs.SetString("CombinedNames", rearrangedNames);
    76.                 PlayerPrefs.Save();
    77.             }
    78.             else
    79.             {
    80.                 Debug.LogError("Insufficient data for rearranging.");
    81.             }
    82.         }
    83.         else
    84.         {
    85.             Debug.LogError("CombinedNames not found in PlayerPrefs!");
    86.         }
    87.      
    88.                     string storedcombinedNames = PlayerPrefs.GetString("CombinedNames", "");
    89.                     // Assuming your videos are in the "Assets/Videos/" directory
    90.                     string videoFilename2 = storedcombinedNames + ".mp4";
    91.  
    92.                     // Set the video URL and play the video
    93.                     videoPlayer.url = "Assets/Videos/IDLE/" + videoFilename2;
    94.                     PlayVideo();
    95.     }
    96.  
    97.     public void RearrangeAndStoreNames2()
    98.     {
    99.         videoPlayer.Stop();
    100.         string storedCombinedNames = PlayerPrefs.GetString("CombinedNames", "");
    101.  
    102.         if (!string.IsNullOrEmpty(storedCombinedNames))
    103.         {
    104.             // Split the names into an array
    105.             string[] namesArray2 = storedCombinedNames.Split(", ");
    106.  
    107.             // Rearrange the array (e.g., switch elements at index 0 and 2)
    108.             if (namesArray2.Length >= 4)
    109.             {
    110.                 string temp2 = namesArray2[1];
    111.                 namesArray2[1] = namesArray2[3];
    112.                 namesArray2[3] = temp2;
    113.  
    114.                 // Join the rearranged array back into a string
    115.                 string rearrangedNames2 = string.Join(", ", namesArray2);
    116.  
    117.                 // Store the rearranged names back to PlayerPrefs
    118.                 PlayerPrefs.SetString("CombinedNames", rearrangedNames2);
    119.                 PlayerPrefs.Save();
    120.             }
    121.             else
    122.             {
    123.                 Debug.LogError("Insufficient data for rearranging.");
    124.             }
    125.         }
    126.         else
    127.         {
    128.             Debug.LogError("CombinedNames not found in PlayerPrefs!");
    129.         }
    130.      
    131.                     string storedcombinedNames = PlayerPrefs.GetString("CombinedNames", "");
    132.                     // Assuming your videos are in the "Assets/Videos/" directory
    133.                     string videoFilename2 = storedcombinedNames + ".mp4";
    134.  
    135.                     // Set the video URL and play the video
    136.                     videoPlayer.url = "Assets/Videos/IDLE/" + videoFilename2;
    137.                     PlayVideo();
    138.     }
    139.  
    140.  
    141.     void OnVideoEnd(VideoPlayer vp)
    142.     {
    143.         string storedcombinedNames = PlayerPrefs.GetString("CombinedNames", "");
    144.         // Assuming your videos are in the "Assets/Videos/" directory
    145.             string videoFilename2 = storedcombinedNames + ".mp4";
    146.  
    147.             // Set the video URL and play the video
    148.             videoPlayer.url = "Assets/Videos/IDLE/" + videoFilename2;
    149.             videoPlayer.Play();
    150.     }
    151. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,467
    This is the 2D forum, it's not for posts about video or general scripting etc.

    Please look at the available forums before posting.

    I'll move your thread for you.

    Thanks.
     
    shampaw likes this.
  3. shampaw

    shampaw

    Joined:
    Dec 3, 2023
    Posts:
    2
    ok, im sorry and thanks☺️
     
    MelvMay likes this.