![raspberry pi media player raspberry pi media player](https://m.media-amazon.com/images/I/51v4k5Tz1SL._SL500_.jpg)
This is Python shorthand to insert a new line between each of the instructions.
Raspberry pi media player code#
So for the try section our code will first print three lines to the shell, these are the instructions to the user. It will try to run the code, but if the user presses CTRL + C then the code will exit. But first we wrap this section in an exception handler. Ok so on to the last part, and this is the part that looks for when a button is pressed, and reacts accordingly. Randomiser = Button(2)stop = Button(3)pause_button = Button(4) These buttons are connected to the GPIO at pins 2, 3 and 4 respectively. We have three buttons connected, randomiser (play), stop and pause_button.
![raspberry pi media player raspberry pi media player](https://i.ytimg.com/vi/1_esHOFe55c/maxresdefault.jpg)
With the functions created, we next need to tell our code where our buttons are connected. The last function is called “pause_video” and it emulates pressing the space bar on the keyboard, which is how omxplayer pauses video.ĭef pause_video(): keyboard.press_and_release('space') For this we use the keyboard library, specifically the “press_and_release” function to simulate pressing the “q” key.ĭef stop_video(): keyboard.press_and_release('q') The next function is called “stop_video” and as you can guess it will stop the currently playing video. 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.
Raspberry pi media player mp4#
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)
![raspberry pi media player raspberry pi media player](http://www.jasonhardin.com/media/diy/IMG_3878.jpg)
Raspberry pi media player full#
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.