制作每小时循环播放不同歌曲的节目[Python]

时间:2017-06-21 04:19:14

标签: python clock

我正在尝试创建一个程序,它将在一天中的每个小时循环播放某首歌曲。例如,从下午12点到下午1点,某首歌将连续播放,然后一旦它转到下午1点,不同的歌曲将持续播放到下午2点。

我对Python很新,所以我不知道从哪里开始。我尝试过做研究,但我找不到多少。为了让你了解我是多么困难,我甚至无法正常播放歌曲。

如果有人能给我一点代码来开始,那将非常感激。我希望我的程序能够参考系统时钟来查找时间,但我不确定它会有多复杂。

很抱歉,如果我听起来像是在试图让别人为我做些事,但我真的无法弄清楚从哪里开始,我迫切希望得到任何帮助

提前致谢!

2 个答案:

答案 0 :(得分:1)

#Open your favorite song on youtube, after every 2 hours and have a break from your work.

import webbrowser
import time
import datetime

total_breaks = 3
break_count = 0

print("The program has started on : "+time.ctime())
while(break_count<total_breaks):
    time.sleep(7200) #interval is of 2 hrs = 7200 seconds
    webbrowser.open('https://www.youtube.com/watch?v=rtOvBOTyX00')
    break_count = break_count + 1

答案 1 :(得分:0)

您可以使用vlc.py文件播放歌曲。从http://git.videolan.org/?p=vlc/bindings/python.git;a=tree;f=generated;b=HEAD获取文件。将其另存为vlc.py

您尚未指定希望播放歌曲的位置。假设你有一个收集mp3的文件夹。我们将播放该文件夹中的每首歌曲一小时。

以下代码应播放每首歌约1小时。

from vlc import *
import time,os


#get a list of all songs in the current directory
songs =  [f for f in os.listdir('.') if f.endswith('mp3')]


#loop over all the songs present in current directory
for song in songs:

    #to play a song using vlc
    p = MediaPlayer(song)   
    p.play()

    #a delay so that attributes of object p can be initialized 
    time.sleep(1)

    #playing song continously for 1 hour from current time.
    starttime = int(time.time())

    while True:
        now = int(time.time())

        #p.is_playing() sets to 1 if the song is being played.
        # Keep looping till song is being played
        if p.is_playing() == 1:
            pass

        #if song stops check if 1 hour is over or not.
        #If not then play again.
        elif (now - starttime) < 3599:
            p.release()
            p = MediaPlayer(song)
            p.play()
            time.sleep(1)

        #if an hour is gone then move on to the next song.
        else:

            p.release()
            break;

    # print "time over"
    # print time.time()

确保将vlc.py,所有mp3歌曲和上述代码的文件保存在同一目录中。