我试图编写代码询问用户要播放哪首歌(在显示的选项列表中),但是问题是vlc模块不想播放这首歌。但是,我尝试了不使用if语句的情况,并且可以正常工作。这是我的第一次编码,如果代码看起来搞砸了,我感到抱歉:
from tinytag import TinyTag as chimera
import vlc
def choice():
try:
print('music1')
print('music2')
print('music3')
music = int(input('choose the song you like\n'))
info(music)
musical(music)
except ValueError:
choice()
def info(music):
try:
if music == 1:
tag = chimera.get('C:/Users/user/music/music1.ogg')
print('song name %s' % tag.title )
print('Duration %f' % tag.duration)
elif music == 2:
tag = chimera.get('C:/Users/user/music/music2.m4a')
print('song name %s' % tag.title)
print('Duration %f' % tag.duration)
elif music == 3:
tag = chimera.get('C:/Users/user/music3.m4a')
print('song name %s' % tag.title)
print('Duration %f' % tag.duration)
else:
print('invalid input')
choice()
except SyntaxError:
pass
def musical(music):
playing = input('do you want to play the music?\n')
if music == 1 and playing == 'yes':
player1 = vlc.MediaPlayer('C:/Users/user/music/music1.ogg')
player1.play()
elif music == 2 and playing == 'yes':
player2 = vlc.MediaPlayer('C:/Users/user/music/music2.m4a')
player2.play()
elif music == 3 and playing == 'yes':
player3 = vlc.MediaPlayer('C:/Users/user/music/music3.m4a')
player3.play()
choice()
答案 0 :(得分:1)
您需要在smallhouse.jpg
处于活动状态时保持程序运行。
我对您的代码做了一些修改,出于我的目的,我删除了vlc
引用。
tinytag
请注意,vlc播放器的import vlc
import time
def choice():
try:
print('music1')
print('music2')
print('music3')
music = int(input('choose the song you like( 1-3 )\n'))
if music < 1 or music > 3:
raise ValueError
musical(music)
except ValueError:
print("Input error\n")
choice()
def musical(music):
if music == 1:
player = vlc.MediaPlayer('C:/Users/user/music/music1.ogg')
elif music == 2:
player = vlc.MediaPlayer('C:/Users/user/music/music2.ogg')
elif music == 3:
player = vlc.MediaPlayer('C:/Users/user/music/music3.ogg')
player.play()
playing = set([1,2,3,4])
time.sleep(0.1)
while True:
state = player.get_state()
if state not in playing:
break
time.sleep(1)
choice()
值为:
state