metalx1000

Godot - Play Random Video with code

Jan 17th, 2021
277
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2. #Copyright Kris Occhipinti 01/16/2021
  3. #https://filmsbykris.com
  4. #Licensed under the GPLv3
  5.  
  6. func _ready():
  7. #load and play random video
  8. var video = get_random_video()
  9. var player = create_video_player(video,self)
  10.  
  11.  
  12. func create_video_player(video,parent):
  13. var player = VideoPlayer.new()
  14. parent.add_child(player)
  15. player.set_size(get_viewport_rect().size)
  16. video = load(video)
  17. player.stream = video
  18. player.play()
  19. return player
  20.  
  21. func get_random_video():
  22. var videos = search_files(".",".ogv")
  23. randomize()
  24. videos.shuffle()
  25. return videos[0]
  26.  
  27. func search_files(path,ext):
  28. var files = []
  29. var dir = Directory.new()
  30. dir.open(path)
  31. dir.list_dir_begin()
  32.  
  33. while true:
  34. var file = dir.get_next()
  35. if file == "":
  36. break
  37. elif file.ends_with(ext):
  38. files.append(file)
  39. dir.list_dir_end()
  40. return files
  41.  
RAW Paste Data