PyAudio로 웨이브를 재생하는 Tkinter 버튼 호출 기능 - 충돌 (Tkinter button calling function to play wave with PyAudio -- crashes)


문제 설명

PyAudio로 웨이브를 재생하는 Tkinter 버튼 호출 기능 ‑ 충돌 (Tkinter button calling function to play wave with PyAudio ‑‑ crashes)

버튼을 누르자 마자 계속 눌러져 있고 프로그램이 충돌합니다. 그래도 소리는 재생됩니다. 저는 PyAudio 사이트에서 바로 코드를 사용하고 있으므로 여기에서 충돌이 발생하는 이유가 약간 혼란스럽습니다.

from tkinter import *
import pyaudio
import wave
import sys

root = Tk()
root.title("Compose‑O‑Matic")
root.geometry("400x300")

def play_audio():
    chunk = 1024
    wf = wave.open('misc_environment3.wav', 'rb')
    p = pyaudio.PyAudio()

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

    data = wf.readframes(chunk)

    while data != '':
        stream.write(data)
        data = wf.readframes(chunk)

    stream.stop_stream()
    stream.close()
    p.terminate()

app = Frame(root)
app.grid()

button_start = Button(app, text = ">", command = play_audio)
button_start.grid()

root.mainloop()

참조 솔루션

방법 1:

Use threading to play music.

from tkinter import *
import pyaudio
import wave
import sys
import threading

# ‑‑‑ classes ‑‑‑

def play_audio():
    global is_playing
    chunk = 1024
    wf = wave.open('misc_environment3.wav', 'rb')
    p = pyaudio.PyAudio()

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

    data = wf.readframes(chunk)

    while data != '' and is_playing: # is_playing to stop playing
        stream.write(data)
        data = wf.readframes(chunk)

    stream.stop_stream()
    stream.close()
    p.terminate()

# ‑‑‑ functions ‑‑‑

def press_button_play():
    global is_playing
    global my_thread

    if not is_playing:
        is_playing = True
        my_thread = threading.Thread(target=play_audio)
        my_thread.start()

def press_button_stop():
    global is_playing
    global my_thread

    if is_playing:
        is_playing = False
        my_thread.join()

# ‑‑‑ main ‑‑‑

is_playing = False
my_thread = None

root = Tk()
root.title("Compose‑O‑Matic")
root.geometry("400x300")

button_start = Button(root, text="PLAY", command=press_button_play)
button_start.grid()

button_stop = Button(root, text="STOP", command=press_button_stop)
button_stop.grid()

root.mainloop()

(by jonnygfuras)

참조 문서

  1. Tkinter button calling function to play wave with PyAudio ‑‑ crashes (CC BY‑SA 2.5/3.0/4.0)

#crash #Python #pyaudio #button #tkinter






관련 질문

WebView가 전체 활동을 죽이고 있습니다 --- 이것을 어떻게 디버깅 할 수 있습니까? (WebView killing the whole activity --- How can I debug this?)

연결 시도 시 Windows의 MySQL이 충돌함 (MySQL on Windows crashes on connection attempts)

휘발성 멤버는 멤버 함수에서 액세스할 수 없습니다. (volatile member can not be accessed by member function)

Ruby에서 Gosu로 텍스트를 인쇄할 수 없음(충돌) (Cannot print a text with Gosu in Ruby (crash))

이 기능은 .exe를 충돌시킵니다. 제가 뭘 잘못하고 있습니까? (This function makes the .exe crash, what am I doing wrong?)

PyAudio로 웨이브를 재생하는 Tkinter 버튼 호출 기능 - 충돌 (Tkinter button calling function to play wave with PyAudio -- crashes)

phonegap 카메라가 앱 충돌 (phonegap camera crashes the app)

대화 상자를 표시하려고 할 때 앱이 충돌함 (App crashed when try to display dialog box)

kaldi 설치 시 libmkl_tbb_thread.so sth 관련 문제 (A problem related to libmkl_tbb_thread.so sth when installing kaldi)

WordPress에서 Simple Jquery Click이 작동하지 않음 (Simple Jquery Click not working in WordPress)

새 줄이 발견되지 않으면 fget이 NULL을 반환하지 않습니다. (fgets dont return NULL when no new line found)

webview가 검은 색으로 바뀌고 응용 프로그램이 충돌합니다. (webview turns black and application crashes)







코멘트