55fb0896b8
Fay2.0: 1、控制器pc内网穿透,音频输入输出设备远程直连; 2、提供android 音频输入输出工程示例代码; 3、提供python音频输入输出工程示例代码(远程PC、树莓派等可用); 4、补传1.0语音指令音乐播放模块(暂不支持远程播放); 5、重构及补充若干工具模块:websocket、多线程、缓冲器、音频流录制器等; 6、修复1.x版本的多个bug。
69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
import os.path
|
|
import random
|
|
import time
|
|
|
|
import eyed3
|
|
import requests
|
|
import re
|
|
import pygame
|
|
|
|
from utils import util
|
|
|
|
__playing = False
|
|
|
|
song_name = ""
|
|
|
|
|
|
def __play_song(song_id: str):
|
|
file_url = "./songs/{}.mp3".format(song_name)
|
|
if not os.path.exists("./songs"):
|
|
os.mkdir("./songs")
|
|
if not os.path.exists(file_url):
|
|
url = "https://music.163.com/song/media/outer/url?id=" + song_id
|
|
response = requests.request("GET", url)
|
|
with open(file_url, "wb") as mp3:
|
|
mp3.write(response.content)
|
|
pygame.mixer.music.load(file_url)
|
|
pygame.mixer.music.play()
|
|
util.log(3, "正在播放 {}".format(song_name))
|
|
audio_length = eyed3.load(file_url).info.time_secs
|
|
last_time = time.time()
|
|
while __playing and time.time() - last_time < audio_length:
|
|
time.sleep(0.05)
|
|
pass
|
|
|
|
|
|
def __random_song():
|
|
# 歌单列表
|
|
id_list = [
|
|
"3778678", # 热歌榜
|
|
# "1978921795", # 电音榜
|
|
# "10520166", # 国电榜
|
|
# "991319590", # 说唱榜
|
|
]
|
|
url = "https://music.163.com/discover/toplist?id=" + id_list[random.randrange(0, len(id_list))]
|
|
response = requests.request("GET", url)
|
|
song_list = re.findall("<li><a href=\"/song\?id=([0-9]*)\">(.*?)</a></li>", response.text)
|
|
index = random.randrange(0, len(song_list))
|
|
return song_list[index]
|
|
|
|
|
|
def play():
|
|
global __playing
|
|
global song_name
|
|
__playing = True
|
|
while __playing:
|
|
song = __random_song()
|
|
try:
|
|
song_name = song[1]
|
|
__play_song(song[0])
|
|
break
|
|
except Exception as e:
|
|
util.log(1, "无法播放 {} 可能需要VIP".format(song[1]))
|
|
|
|
|
|
def stop():
|
|
global __playing
|
|
__playing = False
|
|
pygame.mixer.music.stop()
|