使用树莓派制作自己的广告播放机

准备工作

* 树莓派(Raspberry Pi Zero)一台
* SD卡一张
* 相关配件(电源、显示器、HDMI线、等 ...)
* 如果非HW的树莓派建议联网(USB-WIFI、USB-Ethernet Adapter、SPI-enc28j60)
* 当然也可以使用(3A+、3B+、4B)

系统安装

* 下载镜像 https://www.raspberrypi.org/downloads/raspbian/
* 使用 rufus 或 Win32DiskImager 向 SD卡 写入镜像
* 配置网络、管理员密码、SSH、等 ...
* 以下操作均需要使用 root 下操作 ...

系统设置

分辨率设置

# /boot/config.txt
hdmi_group=2
hdmi_mode=85
# Raspberry Pi Zero 建议 720p 分辨率
# Raspberry Pi 3A+、3B+、4B 建议 1080p 分辨率

显存设置(重启生效)

# /boot/config.txt
gpu_mem=192
# 显存内存分配不能少于192M

多媒体播放程序安装

// 下载安装
git clone https://github.com/nulijiabei/xplay.git
cd xplay/
cd buster/ # Raspbian Buster with desktop
sh -x install

// 运行测试(安装成功则出现欢迎使用画面)
// RPI Zero 建议分辨率 720p
root@raspberrypi:~#/usr/bin/xplay -R 0,0,1280,720
// RPI 3A+、3B+、4B 建议分辨率 1080p
root@raspberrypi:~#/usr/bin/xplay 
root@raspberrypi:~#/usr/bin/xplay -R 0,0,1920,1080 

自定义播放控制

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import socket
import json
import time
import os

def connect():
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(('127.0.0.1', 8700))
    return client

def stop(client):
    data = {}
    params = {}
    params['all'] = True
    data['type'] = "stop"
    data['params'] = params
    js = json.dumps(data)
    client.send((js + '\n#End\n').encode('utf-8'))

def play(client, libName, zIndex, path, top, left, width, height, screenMode, screenRotate):
    data = {}
    params = {}
    params['zIndex'] = zIndex
    params['path'] = path
    params['top'] = top
    params['left'] = left
    params['width'] = width
    params['height'] = height
    params['screen_mode'] = screenMode
    params['screen_rotate'] = screenRotate
    data['id'] = ("PLAY_VIDEO_%d") % (int(round(time.time() * 1000)))
    data['type'] = "play"
    data['libName'] = libName
    data['params'] = params
    js = json.dumps(data)
    client.send((js + '\n#End\n').encode('utf-8'))

def getFiles(str):
    fs = []
    for root,dirs,files in os.walk(str):
    for file in files:
        fs.append(str + '/' + file)
    return fs

if __name__ == '__main__':

    # 建立连接
    client = connect()

    # 停止全部
    stop(client)

    # 循环播放
    while True:

    # 获取目录素材文件
    fs = getFiles(r'E:\test')
    if len(fs) == 0 :
        time.sleep(5)
        continue

    # 遍历素材
    for f in fs:
        # 是否为视频
        if f.endswith(".mp4") :
        # 播放视频
        play(client, "video", 10, f, 0, 0, 1920, 1080, "landscape", 0)
        # 是否为图片
        if f.endswith(".jpg") or f.endswith(".png") :
        # 播放图片
        play(client, "pic", 10, f, 0, 0, 1920, 1080, "landscape", 0)
        # 间隔5s切换
        time.sleep(5)

    # 断开连接
    client.close()

更多

https://github.com/nulijiabei/xplay