如何在等待计时器完成时接收shell中的输入?

2024-10-01 09:34:52 发布

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

我正在用python编写一个音乐播放器。现在我得到歌曲的长度,以秒为单位,然后用这个长度来表示时间。睡眠()功能。问题是我不能在pythonshell中使用命令来停止、暂停、跳过等歌曲。你知道吗

我的程序除了能接受输入外还能工作。有没有办法让程序继续按原来的方式工作,并增加接收输入的能力?基本上我只需要替换我的“时间。睡眠(歌曲长度)“带有一个不停止输入shell的计时器。你知道吗

对不起,没有空格,我有坏习惯。你知道吗

谢谢。你知道吗

# import libs
import os
import pygame
from mutagen.mp3 import MP3
import time
import random

usedAlbums = []
usedSongs = []
VOL = 1
MUSICDIR = '/home/pi/MusicPlayer/Pi/All Songs/'
os.chdir(MUSICDIR)
albums = os.listdir(MUSICDIR)
random.shuffle(albums)
pygame.mixer.pre_init(frequency=22050, size=-16, channels=2, buffer=4096)

# define funcs
def addAlbum():

    currentAlbum = albums[album]

    if currentAlbum in usedAlbums:
        addAlbum()
    else:
        usedAlbums.append(currentAlbum)

    return currentAlbum

def addSong():

    currentSong = songs[song]

    if currentSong in usedSongs:
        addSong()
    else:
         usedSongs.append(currentSong)

    return currentSong

# begin main code
print("~" * 80)
for album in range(len(albums)):
    # get new or starter album(s)
    currentAlbum = addAlbum()
    os.chdir(currentAlbum)
    songs = os.listdir(MUSICDIR + currentAlbum + "/")
    random.shuffle(songs)
    song = 0
    for song in range(len(songs)):
        # get new or starter song(s)
        currentSong = addSong()
        songinfo = MP3(currentSong)
        pygame.mixer.init(frequency=songinfo.info.sample_rate, size=-16, channels=2, buffer=4096)
        pygame.mixer.music.set_volume(VOL)
        pygame.mixer.music.load(currentSong)
        pygame.mixer.music.play()
        # print info
        print("Total Songs Played:", len(usedSongs))
        print("Album:             ", currentAlbum)
        print("Album Songs:       ", len(songs))
        print("Song:              ", currentSong[:-4])
        print("Song Number:       ", song + 1)
        print("Songs Left:        ", len(songs) - (song + 1))
        print("Duration:          ", str(int(songinfo.info.length // 60)) + ":%02d" % int(songinfo.info.length % 60))
        print("~" * 80)
        time.sleep(songinfo.info.length)
    os.chdir(MUSICDIR)

Tags: importinfolensongospygameprintmixer