赞
踩
from pydub import AudioSegment
from pydub.playback import play
song = AudioSegment.from_wav(r"C:\Users\loong\Downloads\zh.wav")
play(song)
from playsound import playsound
playsound(r"voice.wav")
报错:
参考:https://blog.csdn.net/weixin_50836014/article/details/122135430
https://blog.csdn.net/lj606/article/details/122354958
https://stackoverflow.com/questions/68826091/the-specified-device-is-not-open-or-is-not-recognized-by-mci (这个说降版本,绝对路径)
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 263 for command:
open audio123.wav
指定的设备未打开,或不被 MCI 所识别。
解决方法:去C:\Use***\Python310\site-packages\playsound.py 下更改去注释掉uft-16;两个地方修改
报错:ERROR:playsound:
Error 296 for command:
open “D:\llm\edgetts1.wav”
无法在指定的 MCI 设备上播放指定的文件。文件可能已损坏,或格式不对,或没有此格式的文件处理程序可用。
ERROR:playsound:
Error 263 for command:
close “D:\llm\edgetts1.wav”
指定的设备未打开,或不被 MCI 所识别。
WARNING:playsound:Failed to close the file: “D:\llm\edgetts1.wav”
这里是edge tts生成语音文件用playsound来播放;生成如果是wav格式就报错,生成mp3文件就可以正常播放
参考:https://blog.csdn.net/weixin_44493841/article/details/134531993
pygame
import pygame
def my_playsound(filePath):
pygame.init()
track = pygame.mixer.music.load(filePath)
# 播放
pygame.mixer.music.play()
# 等待直到播放结束
while pygame.mixer.music.get_busy():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.mixer.music.stop()
sys.exit()
pygame.quit()
my_playsound(OUTPUT_FILE)
import streamlit as st
##输入字节流
st.audio(audio_bytes) ##展示前端语音控件
离线音频自动播放
import base64
import streamlit as st
## 文件
file_path = "audio123.wav"
with open(file_path, "rb") as f:
audio_bytes = f.read()
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
audio_tag = f'<audio autoplay="true" src="data:audio/wav;base64,{audio_base64}">'
st.markdown(audio_tag, unsafe_allow_html=True)
https://github.com/Joooohan/audio-recorder-streamlit
安装:
pip install audio-recorder-streamlit
使用:
import streamlit as st
from audio_recorder_streamlit import audio_recorder
audio_bytes = audio_recorder(pause_threshold=1.5)
if audio_bytes:
st.audio(audio_bytes, format="audio/wav")
录制保存wav
import streamlit as st
from audio_recorder_streamlit import audio_recorder
def record():
audio_bytes = audio_recorder(text="点击说话", pause_threshold=1.5)
if audio_bytes:
st.audio(audio_bytes)
with open("streamlit_record.wav", mode='bw') as f:
f.write(audio_bytes)
f.close()
return audio_bytes
audio_data = record()
https://github.com/bouzidanas/streamlit-float 另外streamlit-float 可以控制让麦克风图标一直保持在页面具体位置,不随着页面下拉变化
import streamlit as st
from audio_recorder_streamlit import audio_recorder
from streamlit_float import *
# Float feature initialization
float_init()
footer_container = st.container()
def record():
# Create footer container for the microphone
with footer_container:
audio_bytes = audio_recorder(text="点击说话",pause_threshold=1.5)
if audio_bytes:
st.audio(audio_bytes)
with open("streamlit_record.wav", mode='bw') as f:
f.write(audio_bytes)
f.close()
return audio_bytes
audio_data = record()
# Float the footer container and provide CSS to target it with
footer_container.float("bottom: 0rem;")
版本:gradio-4.16.0 在3.38不生效
gr.Audio既可以输入也可以播放这个函数接口
inputs=gr.Audio(type=“file”, label=“上传音频文件”), # 输入组件,允许用户上传音频文件
outputs=gr.Audio(label=“播放音频”, autoplay=True)
import gradio as gr
def greet(name):
return "Hello " + name + "!"
# 创建 Gradio 界面
with gr.Blocks() as demo:
with gr.Row():
inp_ref = gr.Audio(label="请上传3~10秒内参考音频,超过会报错!", sources=["microphone", "upload"], type="filepath")
gr.Markdown("### Greet")
# gr.Input(type="text", label="Name:")
with gr.Row():
gr.Button("Greet")
gr.Markdown("### Response")
# gr.Markdown("### ", output=True)
# 启动应用
demo.launch()
注意:传入 type="filepath"才会有文件路径值
gr.Image(
type=‘numpy’,
label=‘Input Image’
)
gr.Video(
label=‘Input Video’
)
with gr.Row():
input_image_component = gr.Image(
type='numpy',
label='Input Image'
)
output_image_component = gr.Image(
type='numpy',
label='Output Image'
)
with gr.Row():
input_video_component = gr.Video(
label='Input Video'
)
output_video_component = gr.Video(
label='Output Video'
)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。