2016-01-18 68 views
1

因此,我們的任務是編寫一個播放用戶歌曲的短程序,然後讓他們猜測該歌曲的流派,之後該節目詢問他們是否想聽另一首歌曲或者是否想要退出。 我遇到的問題是程序首次播放歌曲時。它首先打印測驗的標題,並詢問用戶是否想要聽歌,或通過輸入「-1」退出測驗。但是,如果用戶選擇播放歌曲,則會播放歌曲,但一旦剪輯結束,它就不會繼續到測驗的下一部分(猜測流派)。該程序只是停留在這個「播放」模式,我什麼也做不了,進入cntrl + C吧。當我在Python中播放WAV時,程序在文件結束時不會停止,程序沒有響應

我已經無情地搜尋了這個問題的答案,我已經寫出了更簡單的程序來看看它是否會繼續播放歌曲,甚至問我的講師關於這個問題(他沒有任何答案對我來說很奇怪)。 這裏是我的代碼(playWav2是用於播放wav文件的pyaudio腳本):

import playWav2 as pw 
#Here I am providing the list of genres and songs for the program to take info from. 
#I am using "s" for the song list, and "i" for the genre list. 
genre_list=['melodic punk','alt rock','drum and bass','house','punk rock'] 
song_list=["Welcome to Paradise.wav","Worry Rock.wav","Propane Nightmares.wav","Lalula.wav","Life During Wartime.wav"] 
i=0 
s=0 
#Here I am providing the participant with the ability to play a song 
decision=input("Guess the genre of the song! (enter any key to play a song. Enter -1 to finish the quiz)") 
if decision == '-1': 
      exit() 
else: 
    pw.play(song_list[s])   


#Here I am showing the participant the list of possible genres to choose from. 
print("heres a list of my favorite genres!") 
print(genre_list) 
#Here the participant guesses the genre  
genre=input("What genre do you think that was?") 
#If the genre is correct, it plays the next song, if it is not in genre_list, it stops.  
while genre == genre_list[i]: 
    print("Great guess! %s is correct!"%(genre)) 
    choice=input("Ok, so now that you got that right, ready to try another? (y/n)") 
    if choice.lower() == 'y': 
     i+=1 
     i%=5 
     pw.play(song_list[s]) 
    else: 
     break 

這裏是代碼playWav2:

""" Play a WAVE file. """ 

import pyaudio 
import wave 

chunk = 1024 

def play(song): 
    wf = wave.open(song, 'rb') 
    p = pyaudio.PyAudio() 

    # open stream 
    stream = p.open(format = p.get_format_from_width(wf.getsampwidth()), 
       channels = wf.getnchannels(), 
       rate = wf.getframerate(), 
       output = True) 

    # read data 
    data = wf.readframes(chunk) 

    # play stream 
    while data != '': 
     stream.write(data) 
     data = wf.readframes(chunk) 
    stream.stop_stream() 
    stream.close() 
    p.terminate() 
+2

什麼是不起作用的最小腳本?請參見[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –

+0

正在搜索[playWav2](https://www.google.co.uk/search?q= playWav2 + python)出現三個鏈接,其中一個是這個問題 –

+0

嘿!這是我製作的一個簡單腳本,用於查看它是否會繼續播放WAV。 '進口playWav2爲PW pw.play( 「歡迎來到Paradise.wav」) 打印( 「有效嗎?」)' – AaronK5521

回答

0

的錯誤不是在上面的代碼,但在你的模塊playWav2

當你列出pyaudio作爲參考,看看The example on the pyaudio page,示例專門的結局..

或提供鱈魚e用於playWav2。

+0

playWav2代碼: ''進口pyaudio 進口波 塊= 1024 DEF播放(歌曲): WF = wave.open(歌曲, 'RB') p = pyaudio.PyAudio() 流= p.open(格式= p.get_format_from_width(wf.getsampwidth()), channels = wf.getnchannels(), rate = wf.getframerate(), output = True) 數據= wf.readframes(塊) 而數據= '': stream.write(數據) 數據= wf.readframes(塊) stream.stop_stream() stream.close() p .terminate()'' – AaronK5521

+0

正如你可能已經知道的那樣,正確的縮進對python是至關重要的。如果評論不允許您使用正確的格式插入代碼,則需要更新您的問題。 – jogco

+0

對不起,我對這個網站是全新的。我如何才能在我的評論中獲得正確的格式? – AaronK5521

相關問題