如何在播放python歌曲的声音文件时逐字逐句地读这首歌

2024-04-18 05:54:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让python在播放歌曲的同时从文本文件中读取一首歌曲。 它读取歌曲的方式有点像portal(Still Alive)中的歌曲正在播放。在

我觉得这有点棘手,但我需要python一次打印出每一个字母的歌曲,同时保持与实际歌曲的节奏。在

任何帮助都将不胜感激。在

问候!在

编辑

import urllib.request # Example URL 

url = "http://ntl.matrix.com.br/pfilho/oldies_list/top/lyrics/black_or_white.txt" # Open URL: returns file-like object 
lyrics = urllib.request.urlopen(url) # Read raw data, this will return a "bytes object" 
text = lyrics.read() # Print raw data 
print(text) # Print decoded data: 
print(text.decode('utf-8'))

编辑:

好吧,所以我认为这个问题的答案可能很宽泛,所以我想做的是:通过从文本文件中读取每个字符来打印每个字符,并设置读取/打印的速度。在


Tags: texturl编辑datarawobjectrequesturllib
1条回答
网友
1楼 · 发布于 2024-04-18 05:54:56

下面是一些基本的代码,假设您手头有很多信息。在

import sys
from time import sleep
# For python 2 import this
#from itertools import izip as zip
# Delay in seconds to wait before printing each *character* of the song lyrics
time_delays = [0.1, 0.1, 0.1, 0.5, 0.2, 0.1, 0.1]
song_lyrics = "thesong"

print "Let's sing a song..."
for song_char, char_delay in zip(song_lyrics, time_delays):
    sleep(char_delay)
    sys.stdout.write(song_char)
    sys.stdout.flush()

希望有帮助。我使用sys.stdout而不是print的原因是它更易于控制,您需要刷新缓冲区,否则屏幕只会在缓冲区已满时更新。在

相关问题 更多 >