我正在try 使用生成器/yield 在Flask 中一个接一个地传输两个音频文件.然而,它只播放第一个文件,而不播放另一个文件.以下是我的代码:
from flask import Flask, send_file, Response
import random
import time
import wave
# Flask constructor takes the name of
# current module (__name__) as argument.
app = Flask(__name__)
# The route() function of the Flask class is a decorator,
# which tells the application which URL should call
# the associated function.
@app.route('/')
# ‘/’ URL is bound with paadas() function.
def paadas():
Approach 2: not working
def generate(files):
for file in files:
yield file.read()
files = []
number = random.randint(1,10)
f1 = open("../numbers/" + str(number) + ".wav", 'rb')
files.append(f1)
times = random.randint(1,10)
f2 = open("../times/" + str(times) + ".wav", 'rb')
files.append(f2)
return Response(generate(files), mimetype='audio/wav')
# main driver function
if __name__ == '__main__':
# run() method of Flask class runs the application
# on the local development server.
app.run()
我错过了什么吗?你可以看到我在https://github.com/sameermahajan/PaadasMLFlaskApp米时try 了三种方法,只有第一种有效,但不是很优雅.如果你想试用这个程序,你可以从https://github.com/sameermahajan/Paadas得到预先录制的"Numbers"和"Times"的音频.这些都是背诵表格格式的马拉提语(一种印度语言)中的数字.