From 2ea3372e6f9ec973085c9650ee2610a2da9b414d Mon Sep 17 00:00:00 2001 From: Chungsub Kim Date: Sat, 7 Mar 2015 18:47:04 +0900 Subject: [PATCH] add random option --- Adafruit_Video_Looper/model.py | 24 ++++++++++++++++-------- Adafruit_Video_Looper/video_looper.py | 3 ++- video_looper.ini | 3 +++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Adafruit_Video_Looper/model.py b/Adafruit_Video_Looper/model.py index 56f2d7d..9e93b48 100644 --- a/Adafruit_Video_Looper/model.py +++ b/Adafruit_Video_Looper/model.py @@ -1,13 +1,16 @@ # Copyright 2015 Adafruit Industries. # Author: Tony DiCola # License: GNU GPLv2, see LICENSE.txt +import random + class Playlist(object): """Representation of a playlist of movies.""" - def __init__(self, movies): + def __init__(self, movies, is_random): """Create a playlist from the provided list of movies.""" self._movies = movies self._index = None + self._is_random = is_random def get_next(self): """Get the next movie in the playlist. Will loop to start of playlist @@ -16,14 +19,19 @@ class Playlist(object): # Check if no movies are in the playlist and return nothing. if len(self._movies) == 0: return None - # Start at the first movie and increment through them in order. - if self._index is None: - self._index = 0 + # Start Random movie + if self._is_random: + self._index = random.randrange(0, len(self._movies)) else: - self._index += 1 - # Wrap around to the start after finishing. - if self._index >= len(self._movies): - self._index = 0 + # Start at the first movie and increment through them in order. + if self._index is None: + self._index = 0 + else: + self._index += 1 + # Wrap around to the start after finishing. + if self._index >= len(self._movies): + self._index = 0 + return self._movies[self._index] def length(self): diff --git a/Adafruit_Video_Looper/video_looper.py b/Adafruit_Video_Looper/video_looper.py index fd6c5a3..c30ef0a 100644 --- a/Adafruit_Video_Looper/video_looper.py +++ b/Adafruit_Video_Looper/video_looper.py @@ -53,6 +53,7 @@ class VideoLooper(object): self._reader = self._load_file_reader() # Load other configuration values. self._osd = self._config.getboolean('video_looper', 'osd') + self._is_random = self._config.getboolean('video_looper', 'is_random') # Parse string of 3 comma separated values like "255, 255, 255" into # list of ints for colors. self._bgcolor = map(int, self._config.get('video_looper', 'bgcolor') \ @@ -109,7 +110,7 @@ class VideoLooper(object): if re.search('\.{0}$'.format(ex), x, flags=re.IGNORECASE)]) # Create a playlist with the sorted list of movies. - return Playlist(sorted(movies)) + return Playlist(sorted(movies), self._is_random) def _blank_screen(self): """Render a blank screen filled with the background color.""" diff --git a/video_looper.ini b/video_looper.ini index a398bee..220a669 100644 --- a/video_looper.ini +++ b/video_looper.ini @@ -33,6 +33,9 @@ file_reader = usb_drive osd = true #osd = false +# To play random playlist. +is_random = false + # Change the color of the background that is displayed behind movies (only works # with omxplayer). Provide 3 numeric values from 0 to 255 separated by a commma # for the red, green, and blue color value. Default is 0, 0, 0 or black.