前段时间我制作一个给小学生练习100以内加减乘除的安卓app:制作一个给小学生练习100以内四则运算的app-优快云博客,后来发现没有发出声音,不能知道孩子的学习情况,为此我制作了一个有朗读题目的app,这样小学生会更加喜欢它。
一、生成声音文件和声音时长数据
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 9 08:58:22 2024
@author: YBK
"""
import pyttsx3
import sqlite3
import wave
def get_wav_duration(file_path):
with wave.open(file_path, 'r') as wav_file:
frames = wav_file.getnframes()
rate = wav_file.getframerate()
duration = frames / float(rate)
return round(duration,3)
'''
#创建记录0~100数字的声音时长数据表
conn = sqlite3.connect('D:\\bee\\qwsx\\src\\qwsx\\resources\\qwsx.db',timeout=10, check_same_thread=False)
c = conn.cursor()
# 创建TTS引擎对象
number = 0
while number <= 100:
sql = 'INSERT INTO voice (number,sc) VALUES (?,0);'
c.execute(sql, (number,))
conn.commit()
number = number + 1
c.close()
conn.close()
'''
'''
#生成0~100的声音文件并获取播放时长到数据表
conn = sqlite3.connect('qwsx.db',timeout=10, check_same_thread=False)
c = conn.cursor()
engine = pyttsx3.init()
engine.setProperty('audio_format', 'wav')
engine.setProperty('volume', 1.2)
number = 0#1,1001,2001
while number <= 100:
text = str(number)
fi = './'+str(number)+'.wav'
engine.save_to_file(text, fi)
engine.runAndWait()
duration = get_wav_duration(fi)
print(f"The duration of the WAV file is: {duration} seconds.")
query_sql1 = "update voice set sc = ? where number= ?"
c.execute(query_sql1, (duration,number,))
conn.commit()
number = number + 1
input("please input any key to exit!")
c.close()
conn.close()
'''
#生成+-*/=等声音文件并获取播放时长
engine = pyttsx3.init()
engine.setProperty('audio_format', 'wav')
engine.setProperty('volume', 1.2)
text = '填符号'
fi = './tian.wav'
#加 0.92 减 0.948 乘 0.975 除 0.975 等于 1.161 多少 1.066 填符号 1.302
engine.save_to_file(text, fi)
engine.runAndWait()
duration = get_wav_duration(fi)
print(f"The duration of the WAV file is: {duration} seconds.")
二、更新原主程序
from functools import partial
import toga
from toga.style.pack import COLUMN, LEFT, RIGHT, ROW, Pack
import sqlite3
import random
from time import time,sleep
from pathlib import Path
import datetime
import shutil
import miniaudio
import threading
#导入必要的库
resources_folder = Path(__file__).joinpath("../resources/").resolve()
db_filepath = resources_folder.joinpath("qwsx.db")
#db_filepath = '/storage/emulated/0/Pictures/qwsx.db'
#上面是数据库的存放位置,生成手机app请参考我以前的文章
#oppo版 需要用电脑调试应删除下面5行
'''
if Path('/storage/emulated/0/Documents/qwsx.db').exists():
db_filepath = '/storage/emulated/0/Documents/qwsx.db'
else:
shutil.copy(db_filepath, "/storage/emulated/0/Documents/qwsx.db")
db_filepath = '/storage/emulated/0/Documents/qwsx.db'
'''
names = globals()
nd = 0
hd = 0 #回答为0为出题,回答为1为1位数答题,2为2位数答题,3为3位数答题,4为符号答题
dan = 101 #初始答案
dan1 = 0
dan2 = 0
dan3 = 0
tjcs = ''
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
#判断是不是数字
def bfnum(number):
conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
c = conn.cursor()
cursor = c.execute("SELECT sc from voice where number = ?;",(number,))
row = cursor.fetchone()
sc = row[0]
c.close()
conn.close()
stream = miniaudio.stream_file(str(resources_folder.joinpath(str(number) + '.wav')))
with miniaudio.PlaybackDevice() as device:
device.start(stream)
sleep(sc)
def bffh(fh):
if fh == '+':
stream = miniaudio.stream_file(str(resources_folder.joinpath('jia.wav')))
with miniaudio.PlaybackDevice() as device:
device.start(stream)
sleep(0.92)
if fh == '-':
stream = miniaudio.stream_file(str(resources_folder.joinpath('jian.wav')))
with miniaudio.PlaybackDevice() as device:
device.start(stream)
sleep(0.948)
if fh == '*':
stream = miniaudio.stream_file(str(resources_folder.joinpath('sheng.wav')))
with miniaudio.PlaybackDevice() as device:
device.start(stream)
sleep(0.975)
if fh == '/':
stream = miniaudio.stream_file(str(resources_folder.joinp