2017-06-21 33 views
0

我想製作一個程序,在當天的每個小時循環播放某首歌曲。例如,從下午12點到下午1點,某首歌曲將繼續播放,然後一旦變爲下午1點,另一首歌曲將持續播放至下午2點。製作播放不同歌曲的程序,每小時循環播放[Python]

我對Python相當陌生,所以我不知道從哪裏開始。我試過做研究,但我找不到太多。爲了讓你知道我有多困難,我甚至無法正常播放歌曲。

如果有人可以給我一小段代碼來開始使用,將不勝感激。我想爲我的程序引用系統時鐘來查找一天中的時間,但我不確定這有多複雜。

對不起,如果我聽起來像我只是想讓別人爲我做點東西,但我真的不知道從哪裏開始,我渴望任何幫助。

在此先感謝!

回答

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 
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歌曲和上述代碼的文件保存在同一目錄中。