Python은 간단한 오디오 플레이어를 구현합니다.

1. 사용해야 하는 Python 라이브러리

  • 파이 게임
  • 티킨터

2. 심플한 UI 디자인

audio_player = Tk()
audio_player.title('Audio Player v1.0')
audio_player.geometry('100x100+570+200')
audio_player.maxsize(height=110, width=220)
audio_player.minsize(height=110, width=220)

3. 기능 모듈 구현

3.1 재생할 오디오 파일 선택

def selectFile():
    file = filedialog.askopenfile(mode='r', filetypes=[('AudioFile', '*.mp3')])
    global filePath
    filePath = str(file).split("'")[1]
    try:
        playAudio()
    except:
        pass

3.2 오디오 재생 제어, 일시 중지

def changeText(text):
    if text == 'play':
        return 'pause'
    if text == 'pause':
        return 'play'


def playStop():
    playBtn.config(text=changeText(playBtn.config('text')[4]))
    if playBtn.config('text')[4] == 'pause':
        mixer.music.unpause()
    else:
        if playBtn.config('text')[4] == 'play':
            mixer.music.pause()

3.3 오디오 볼륨 제어

여기에서 값 0.5로 초기화된 전역 변수 x를 정의할 수 있습니다.

def audioINC(y):
    mixer.music.set_volume(y + 0.1)
    global x
    x += 0.1


def audioDEC(y):
    mixer.music.set_volume(y - 0.1)
    global x
    x -= 0.1

3.4 플레이어 초기화 및 기타 세부 정보

def playAudio():
    try:
        mixer.init()
        mixer.music.load(filePath)
        mixer.music.set_volume(x)
        playBtn.config(text='pause')
        mixer.music.play()
    except:
        pass

4. 달리기

frame = Frame(app)
frame.place(x=35, y=20)

openBtn = Button(frame, text='OpenFile', command=selectFile, width=8).grid(row=0, column=1)

audioDec = Button(frame, text='➖', command=lambda: audioDEC(x)).grid(row=1, column=0)
playBtn = Button(frame, text='...', command=playStop, width=8)
playBtn.grid(row=1, column=1)
audioInc = Button(frame, text='➕', command=lambda: audioINC(x)).grid(row=1, column=2)
restartBtn = Button(frame, text='Restart', command=playAudio, width=8).grid(row=2, column=1)

app.mainloop()

5. 간단한 오디오 플레이어 표시

여기에 이미지 설명 삽입
① "OpenFile" 버튼을 클릭하여 로컬 오디오 파일을 엽니다.
② "➖" 및 "➕"는 각각 볼륨의 감소 및 증가를 제어합니다.
③ "다시 시작" 버튼을 클릭하여 현재 선택된 오디오를 재생합니다.

6. 요약

이 기사는 간단한 오디오 플레이어만 구현하고 UI는 매우 간단하며 오디오 재생 기능만 구현하기 위해 학습 참조용입니다.

Supongo que te gusta

Origin blog.csdn.net/hutianle/article/details/123204633
Recomendado
Clasificación