Python 定时播放音乐(公司里用来轻松一下)

具备跨平台能力,目前在一台树莓派3B+上使用,带着公司音响,每天定时做操,放音乐

import os
import random
import re
import sys
import time
from datetime import datetime

# 输出时间
# from apscheduler.schedulers.background import BackgroundScheduler

import pygame
import requests
import simpleaudio
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.blocking import BlockingScheduler
from bs4 import BeautifulSoup

_dir = os.path.dirname(os.path.abspath(__file__))
playlist_path = os.path.join(_dir, 'media')
# playlist_path = '/Users/haohu/Music/酷狗音乐/BQ/'
mlist = []
m_play_list = []
MUSIC_END = pygame.USEREVENT + 1


def random_file():
    rad = random.randint(0, len(mlist) - 1)
    print(rad)
    mfile = mlist[rad]
    return mfile


def load_play_list():
    print("load_play_list")
    for iroot, idir, flist in os.walk(playlist_path):
        for f in flist:
            if f.find("mp3") != -1:
                mlist.append(os.path.join(iroot, f))
            else:
                print("No Mp3 !")

    print(mlist)
    play_music()


def search_music():
    print("search_music")


def job():
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))


def start_exercises():
    print("start_exercises")
    stop_music()
    wave_obj = simpleaudio.WaveObject.from_wave_file(playlist_path + "/zuocao.wav")
    play_obj = wave_obj.play()
    play_obj.wait_done()


def play_music():
    print("music first_play")
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

    mfile = random_file()
    m_play_list.append(mfile)
    print(mfile)
    pygame.mixer_music.fadeout(1)
    pygame.mixer_music.load(mfile)
    pygame.mixer.music.set_volume(0.1)
    pygame.mixer.music.set_endevent(MUSIC_END)
    pygame.mixer.music.play()

    for num in range(1, 11):
        time.sleep(0.5)
        vol = num / 10
        print(vol)
        pygame.mixer.music.set_volume(vol)


def continue_music():
    print("continue_music")
    mfile = random_file()
    m_play_list.append(mfile)
    print(mfile)
    pygame.mixer_music.fadeout(1)
    pygame.mixer_music.load(mfile)
    pygame.mixer.music.set_volume(1)
    pygame.mixer.music.set_endevent(MUSIC_END)
    pygame.mixer.music.play()


def end_music():
    print("end_music")
    # pygame.mixer.music.stop()
    pygame.mixer.music.set_endevent()


def stop_music():
    print("end_music")
    pygame.mixer.music.stop()


def main():
    # 实例化调度器
    scheduler = BackgroundScheduler()
    # 执行时先remove所有
    print("main")
    scheduler.remove_all_jobs()
    # scheduler.add_jobstore()
    # MongoDBJobStore
    # scheduler.add_job(job, 'interval', minutes=1, jitter=100, misfire_grace_time=200, max_instances=1)
    scheduler.add_job(load_play_list, 'cron', day_of_week='0-6', hour=13, minute=25)
    scheduler.add_job(start_exercises, 'cron', day_of_week='0-6', hour=13, minute=29)
    scheduler.add_job(load_play_list, 'cron', day_of_week='0-6', hour=15, minute=15)
    scheduler.add_job(end_music, 'cron', day_of_week='0-6', hour=15, minute=30)
    scheduler.add_job(load_play_list, 'cron', day_of_week='0-6', hour=16, minute=16)
    scheduler.add_job(end_music, 'cron', day_of_week='0-6', hour=16, minute=31)

    # scheduler.add_job(wake_up, 'cron', day_of_week='0-6', hour=11, minute=38)
    # scheduler.add_job(start_exercises, 'cron', day_of_week='0-6', hour=11, minute=39)
    # scheduler.add_job(end_music, 'cron', day_of_week='0-6', hour=11, minute=10)
    # scheduler.add_job(job, 'interval', seconds=3)
    scheduler.start()

    # pygame.init()
    print("pygame_init")
    pygame.mixer.init()
    pygame.display.set_mode([500, 300])
    # pygame 事件轮询
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == MUSIC_END:
                print('music end event')
                continue_music()


if __name__ == '__main__':
    print(playlist_path)
    # load_play_list()
    main()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值