add random option

This commit is contained in:
Chungsub Kim 2015-03-07 18:47:04 +09:00
parent 2a477efdfc
commit 2ea3372e6f
3 changed files with 21 additions and 9 deletions

View File

@ -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):

View File

@ -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."""

View File

@ -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.