python播放wav音频
时间: 2023-10-14 16:28:34 浏览: 95
确实很简单,你可以使用Python自带的wave库来实现。可以使用下面的代码播放.wav音频文件:
```python
import wave
import pyaudio
# 打开wav文件
wf = wave.open('audio.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(1024)
while data != b'':
stream.write(data)
data = wf.readframes(1024)
# 关闭音频输出对象和wav文件
stream.stop_stream()
stream.close()
p.terminate()
wf.close()
```
请注意,要运行这段代码,请先安装pyaudio和wave库。
相关问题
如何使用python播放wav音频
您可以使用Python的wave模块来播放wav音频。首先,导入wave模块:
```
import wave
```
然后,使用wave.open()函数打开要播放的wav文件,例如:
```
wav = wave.open('audio.wav', 'rb')
```
接下来,使用PyAudio库播放音频。如果你没有安装PyAudio库,可以使用以下命令进行安装:
```
pip install pyaudio
```
最后,定义一个播放函数来播放音频:
```
import pyaudio
def play_audio(wav):
chunk = 1024
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wav.getsampwidth()),
channels=wav.getnchannels(),
rate=wav.getframerate(),
output=True)
data = wav.readframes(chunk)
while data:
stream.write(data)
data = wav.readframes(chunk)
stream.stop_stream()
stream.close()
p.terminate()
```
调用这个函数就可以播放音频了:
```
play_audio(wav)
```
用python播放wav音频并且带有进度条播放按钮
以下是一个示例代码,使用pydub库实现了wav音频的播放,并使用tkinter库创建了一个简单的GUI界面,包括一个进度条和播放按钮。
```python
from tkinter import *
from tkinter import filedialog
from pydub import AudioSegment
from pydub.playback import play
import threading
class AudioPlayer:
def __init__(self, file_path):
self.audio = AudioSegment.from_wav(file_path)
self.playing = False
def play(self, start_time, end_time):
if self.playing:
return
self.playing = True
if end_time == -1:
end_time = len(self.audio)
audio_slice = self.audio[start_time:end_time]
play(audio_slice)
self.playing = False
class PlayerGUI:
def __init__(self):
self.root = Tk()
self.root.title("WAV Player")
self.create_widgets()
self.audio_player = None
def create_widgets(self):
self.file_label = Label(self.root, text="No File Selected")
self.file_label.pack()
self.select_button = Button(self.root, text="Select File", command=self.select_file)
self.select_button.pack()
self.progress_bar = Progressbar(self.root, orient=HORIZONTAL, length=300, mode='determinate')
self.progress_bar.pack()
self.play_button = Button(self.root, text="Play", command=self.play_audio)
self.play_button.pack()
def select_file(self):
file_path = filedialog.askopenfilename()
if file_path:
self.file_label.config(text=file_path)
self.audio_player = AudioPlayer(file_path)
def play_audio(self):
start_time = 0
end_time = -1
self.progress_bar['value'] = 0
if self.audio_player:
t = threading.Thread(target=self.audio_player.play, args=(start_time, end_time))
t.start()
self.update_progress()
def update_progress(self):
if self.audio_player and self.audio_player.playing:
current_time = self.audio_player.audio.tell()
total_time = len(self.audio_player.audio)
progress = int(current_time / total_time * 100)
self.progress_bar['value'] = progress
self.root.after(100, self.update_progress)
def run(self):
self.root.mainloop()
if __name__ == '__main__':
gui = PlayerGUI()
gui.run()
```
该代码创建了两个类:`AudioPlayer`用于处理音频文件,`PlayerGUI`用于创建GUI界面。在`AudioPlayer`类中,使用pydub库读取wav文件,并提供了`play()`方法用于播放音频。在`PlayerGUI`类中,使用tkinter库创建了一个窗口,包括一个标签、一个按钮、一个进度条和一个播放按钮。单击“Select File”按钮可以选择要播放的wav文件。单击“Play”按钮可以播放选定的音频文件,并在进度条上显示播放进度。`update_progress()`方法使用`after()`函数周期性地更新进度条。整个应用程序使用`run()`方法运行。
请注意,该示例代码仅用于演示如何使用Python播放wav音频并在GUI中显示进度条和播放按钮。实际应用程序可能需要更复杂的功能,例如暂停、停止、重置等。