![]() The last line in this function uses the Popen class from the subprocess library to run a command as if we were sat at the terminal, and this command is to open the omxplayer media player, and then play the chosen video. Then we shall choose a random video from the list and store it in a variable called “chosen”, again we print this to the Python shell chosen = choice(videos) print(chosen) print(chosen) Then we shall choose a random video from the list and store it in a variable called “chosen”, again we print this to the Python shell. So now let’s check that the list has been populated with file names by printing the contents of the list to the Python shell. Everytime an mp4 is found, it is appended to the “videos” list we have just created.įor file in glob.glob("/media/pi/Videos/*.mp4"): videos.append(file) ![]() Change this to match the name of your chosen directory full of videos. ![]() For this we use “glob” to access the directory “/media/pi/Videos” which is really a USB stick called “Videos” full of mp4 files. ![]() To fill the list with information, specifically a list of all the videos we can play, we use a for loop, that will iterate over every file in a directory, as long as it is an mp4 video file. The first function is called “play_video” and it first creates a list (a data storage object) called “videos”. Moving on we create three functions, blocks of code that we can later reuse by calling their name. Import globimport subprocessimport keyboard The final three imports are a library that we shall use to list the contents of a directory (glob), run terminal commands (subprocess) and the last is a library to emulate a keyboard (keyboard.) ![]() The first three libraries are used to detect the button presses (GPIO Zero), stop the code from running once and exiting (pause) and to choose a random video (choice.)įrom gpiozero import Buttonfrom signal import pausefrom random import choice So now we start writing the code, and our first act is to import the libraries that we require. Save the code as VideoPlayer.py and remember to save often! Immediately click File > Save in the new window. For now open the Python 3 editor, found in the Programming menu and click on File > New to create a new blank file. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |