GPIO triggered playback

In this tutorial we will change the functionality of the player, so it will boot into a blank screen, and when we push an external button, video will start. Find the Code on GitHub/JuliusCode/mp4museum

Installation Instructions

  1. Connect to Raspberry Pi: Use SSH to connect to your Raspberry Pi.
   ssh pi@<your_pi_ip_address>

Password: mp4museum

  1. Disable Overlay Filesystem:
  • Run the following command to open the Raspberry Pi configuration tool:
   sudo raspi-config
  • Navigate to the option to disable the overlay filesystem and reboot your Raspberry Pi.
  1. File Placement:
  • Place this script (mp4m-gpio.py) and your video files in the /home/pi directory.
  1. Edit .bashrc:
  • Open the .bashrc file for editing:
   nano /home/pi/.bashrc
  • Add the following line at the end of the file:
   python3 /home/pi/mp4m-gpio.py > /tmp/mp4museum.log 2>&1
  • Save and exit the editor.
  1. Reboot: Finally, reboot your Raspberry Pi to apply the changes.

Connecting a Switch to Ground

To connect a switch to the Raspberry Pi GPIO:

  1. Identify GPIO Pins: The GPIO pins on the Raspberry Pi are numbered. For this script, you will use GPIO pins 13, 14, 22, and 23.
  2. Wiring:
  • Connect one terminal of the switch to the GPIO pin (e.g., GPIO 22 for stop playback).
  • Connect the other terminal of the switch to the ground (GND) pin on the Raspberry Pi.
  1. Pull-Down Resistor: The script uses internal pull-down resistors, so no external resistors are needed.
Image copyright and source:https://www.raspberrypi.com/documentation/computers/raspberry-pi.html

Adding Videos to the Script Array

To add more videos to the script:

  1. Locate the gpio_map Dictionary: This dictionary maps GPIO pins to video files.
  2. Add New Entries: For each new video, add a new entry in the format:
   gpio_map[<GPIO_PIN>] = '<video_file_name.mp4>'

For example, to add a video for GPIO pin 15:

   gpio_map[15] = 'video3.mp4'
  1. Ensure Video Files are in the Correct Directory: Make sure the video files are located in the /home/pi directory.

Code Breakdown

1. Importing Libraries

import time
import vlc
import os
import RPi.GPIO as GPIO
  • time: Used for adding delays.
  • vlc: Library for playing video files.
  • os: Used for file operations.
  • RPi.GPIO: Library for controlling GPIO pins on the Raspberry Pi.

2. Audio Device Configuration

audiodevice = "0"
if os.path.isfile('/boot/alsa.txt'):
    with open('/boot/alsa.txt', 'r') as f:
        audiodevice = f.read(1)
  • This segment checks for an audio device configuration file and reads the audio device ID.

3. VLC Play Function

def vlc_play(source):
    ...
  • This function initializes the VLC player, plays the specified media file, and waits for it to finish while checking for GPIO events.

4. GPIO Event Handler

def handle_gpio_event():
    ...
  • This function checks the state of the GPIO pins. It stops playback, toggles pause/play, or plays a video based on which pin is pressed.

5. GPIO Pin Mapping

gpio_map = {
    13: 'video1.mp4',
    14: 'video2.mp4',
    ...
}
  • This dictionary maps GPIO pins to specific video files.

6. GPIO Setup

GPIO.setmode(GPIO.BOARD)
try:
    ...
except KeyboardInterrupt:
    ...
finally:
    GPIO.cleanup()
  • This segment sets up the GPIO pins, enters a loop to handle events, and ensures GPIO settings are cleaned up on exit.