python - 当我在 python 中播放 WAV 时,文件结束时程序不会停止,导致程序无响应

标签 python audio wav pyaudio

因此,我们的任务是编写一个简短的程序,为用户播放歌曲,然后要求他们猜测歌曲的流派,之后程序询问他们是否想听另一首歌曲或者是否想退出。 我遇到的问题是程序第一次播放歌曲时。它首先打印出测验的标题,并询问用户是否想听歌曲,或者输入“-1”退出测验。但是,如果用户选择播放歌曲,它会播放该歌曲,但在剪辑结束后不会继续进行测验的下一部分(猜测流派)。程序只是停留在“播放”模式,除了输入 cntrl+C 之外我什么也做不了。

我一直在不懈地寻找这个问题的答案,我写了一些更简单的程序只是为了看看它是否会继续播放这首歌,甚至向我的讲师询问这个问题(他没有任何答案)我很奇怪)。 这是我的代码(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

这是播放Wav2的代码:

""" 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()

最佳答案

是的,上面的代码永远不会离开循环。有点晚了,但尝试在返回 b'' (空字节字符串)时退出它。这对我有用。 (我必须将 wf 更改为 sound)。

sound = wave.open("your.wav")
p = pyaudio.PyAudio()
chunk = 1024
stream = p.open(format =
                p.get_format_from_width(sound.getsampwidth()),
                channels = sound.getnchannels(),
                rate = sound.getframerate(),
                output = True)
data = sound.readframes(chunk)
while True:
    if data != '':
        stream.write(data)
        data = sound.readframes(chunk)

    if data == b'':
        break


关于python - 当我在 python 中播放 WAV 时,文件结束时程序不会停止,导致程序无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34856992/

相关文章:

python - 追加到固定大小的列表

python - 如何使用python进行输出?

Python 等价于 Perl 的证明

c# - 以异步方式刷新 Xamarin.Android 中的 AudioTrack

audio - 3D 音频引擎

javascript - Web Audio API - Javascript 创建的 WAV 文件长度不正确并且无声

python - 本地 HTML 文件无法正确加载到 Dash 应用程序中

javascript - <audio> 到波形或频谱图图像,无需在 Chrome 中播放

header - 为什么整数字节向后存储?这仅适用于标题吗?

java wav播放器添加暂停和继续