前提・実現したいこと
Flaskで音楽ファイルを再生するシステムを作っています。
音楽再生開始後にPLAYボタンを無効にし、
再生完了後にPLAYボタンを有効にする機能を実装中に
以下の問題が発生しました。
目的は、「再生完了時にPLAYボタンが再び有効にしたい。」
発生している問題・再生完了のcallbackメッセージまで問題ないですが、
下記の該当ソースでredirect部分が正常に動作しません。(呼び出されない)
^C(venv) pi@raspberrypi:~/hello_flask/sound $ python fl_play_all_format.py * Serving Flask app "fl_play_all_format" (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 237-014-604 [2020-07-21 22:34:22,521] INFO in fl_play_all_format: 1 192.168.0.108 - - [21/Jul/2020 22:34:22] "GET /1 HTTP/1.1" 200 - 192.168.0.108 - - [21/Jul/2020 22:34:28] "POST /showfile HTTP/1.1" 200 - Finish [2020-07-21 22:34:44,024] INFO in fl_play_all_format: callback
該当のソースコード
Python
1ソースコード 2def wrap_play(filename): 3 snd = mpxplayer(filename) 4 re = snd.run() 5 app.logger.info("callback") 6 return redirect(url_for("index", val=1), code=307) #ここが正常に動作しない 7
試したこと(Pythonソース全体)
from flask import Flask, render_template, request, redirect, url_for from MPXPLAYER import mpxplayer from multiprocessing import Process import glob app = Flask(__name__) def getmusicInfo(): list = {} cnt = 0 for file in glob.glob("/mnt/samba/*.mp*"): list[cnt] = file cnt = cnt + 1 return list @app.route("/<val>") def index(val): app.logger.info(val) val = int(val) return render_template("mpindex.html", flag = val, selectedval = 0, musiclink=getmusicInfo()) def wrap_play(filename): snd = mpxplayer(filename) re = snd.run() app.logger.info("callback") return redirect(url_for("index", val=1), code=307) @app.route("/showfile", methods=['GET','POST']) def main(): wavFile= 0 global play if request.method == 'POST': if request.form['bt'] =='PLAY': wavFile = str(request.form["musicfile"]) musicFile = getmusicInfo()[int(wavFile)] play = Process(name="play", target=wrap_play, args=(musicFile, )) play.start() return render_template("mpindex.html", flag = 0, selectedval = int(wavFile), musiclink=getmusicInfo()) if __name__ == "__main__": app.run(debug=True, host='0.0.0.0')
補足情報(HTMLソース)
HTML
1<!DOCTYPE html> 2<html lang="ja"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 8 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 9 </head> 10 <body> 11 <div class="container"> 12 <div class="header"> 13 <h3 class="text-muted">Music Page</h3> 14 <form action = '/showfile' method='POST'> 15 <select name="musicfile" style="font-size:16px;" id="listbox"> 16 {% for key, value in musiclink.items() %} 17 {% if selectedval == key %} 18 <option value = "{{key}}" selected>{{value}}</option> 19 {% else %} 20 <option value = "{{key}}" > {{value}}</option> 21 {% endif %} 22 {% endfor %} 23 </select> 24 {% if flag == 1 %} 25 <input type="submit" name="bt" value="PLAY" id="bt1"></input> 26 {% else %} 27 <input type="submit" name="bt" value="PLAY" id="bt1" disabled></input> 28 {% endif %} 29 </form> 30<script type="text/javascript" language="javascript"> 31 var select = document.getElementById( 'listbox' ); 32 select.onchange = function(){ 33 document.getElementById("bt1").disabled = false; 34 } 35</sciprt> 36 </div> 37 </div> 38 </body> 39</html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。