使用FLASK构建的Webapp可以创建音频(键入mp3),但可以';以后不要播放那个文件
我用FLASK创建了一个webapp。它使用文本到语音的GoogleAPI生成一个名为“output.mp3”的音频输出。我已将output.mp3放在static文件夹中,然后尝试使用audio.html以下命令播放它:使用FLASK构建的Webapp可以创建音频(键入mp3),但可以';以后不要播放那个文件,flask,audio,google-api,text-to-speech,html5-audio,Flask,Audio,Google Api,Text To Speech,Html5 Audio,我用FLASK创建了一个webapp。它使用文本到语音的GoogleAPI生成一个名为“output.mp3”的音频输出。我已将output.mp3放在static文件夹中,然后尝试使用audio.html以下命令播放它: <audio id="myAudio"> <source src="static/output.mp3" type="audio/mpeg"> </audio> <
<audio id="myAudio">
<source src="static/output.mp3" type="audio/mpeg">
</audio>
<p>Click the buttons to play or pause the audio.</p>
<button onclick="playAudio()" type="button">Play Audio</button>
<button onclick="pauseAudio()" type="button">Pause Audio</button>
<script>
var x = document.getElementById("myAudio");
function playAudio() {
x.play();
}
function pauseAudio() {
x.pause();
}
</script>
如果我不使用“output.mp3”,而是随机播放静态文件夹中的mp3,它就可以正常工作。但它不能播放output.mp3
@app.route('/audio')
def audio():
VideoCamera().__del__()
VideoCamera().closefile()
"""Synthesizes speech from the input string of text or ssml.
Note: ssml must be well-formed according to:
https://www.w3.org/TR/speech-synthesis/
"""
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# with open('flaskblog/output.txt', 'r') as file:
# data = file.read().replace('\n', '')
# Set the text input to be synthesized
global data
synthesis_input = texttospeech.SynthesisInput(text=data)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# The response's audio_content is binary.
with open("flaskblog/static/output.mp3", "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
return render_template('audio.html')