import time from gtts import gTTS from pydub import AudioSegment import playsound def create_audio(text, filename): # 使用 gTTS 创建音频文件 tts = gTTS(text, lang='zh') tts.save(filename) print(f"音频文件已保存为:{filename}") def change_speed(input_filename, output_filename, speed=1.0): # 使用 pydub 加载音频文件 sound = AudioSegment.from_file(input_filename) # 改变音频的播放速度 sound_with_changed_speed = sound.speedup(playback_speed=speed) # 保存改变后的音频文件 sound_with_changed_speed.export(output_filename, format="mp3") print(f"速度改变后的音频已保存为:{output_filename}") def play_audio(filename): playsound.playsound(filename) # 需要播报的文本 text = "非法领用,请将试剂放回柜体" # 初始保存的文件名 initial_file = "initial_warning_message.mp3" # 调整速度后的文件名 final_file = "final_warning_message.mp3" # 创建并保存音频文件 # create_audio(text, initial_file) # 调整音频速度 # change_speed(initial_file, final_file, speed=1.5) # 播放调整速度后的音频文件 # play_audio(final_file) import simpleaudio as sa wave_obj = sa.WaveObject.from_wave_file(r"C:\dev\rms-backend\static\warning_message.wav") play_obj = wave_obj.play() # play_obj.wait_done() # 等待直到声音播放完毕 time.sleep(2)