添加语音播放模块实现部分。
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Sat Feb 28 12:14:20 GMT 2026
|
#Sat Feb 28 13:13:09 GMT 2026
|
||||||
stageCount=9
|
stageCount=9
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.15
|
baseVersion=15.15
|
||||||
publishVersion=15.15.8
|
publishVersion=15.15.8
|
||||||
buildCount=3
|
buildCount=11
|
||||||
baseBetaVersion=15.15.9
|
baseBetaVersion=15.15.9
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import android.os.Handler;
|
|||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
import cc.winboll.studio.powerbell.models.NotificationMessage;
|
import cc.winboll.studio.powerbell.models.NotificationMessage;
|
||||||
|
import cc.winboll.studio.powerbell.models.ThoughtfulServiceBean;
|
||||||
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
import cc.winboll.studio.powerbell.services.ControlCenterService;
|
||||||
|
import cc.winboll.studio.powerbell.threads.TTSRemindThread;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -188,6 +188,9 @@ public class RemindThread extends Thread {
|
|||||||
LogUtils.d(TAG, String.format("未有合适类型提醒,退出提醒线程 | threadId=%d", getId()));
|
LogUtils.d(TAG, String.format("未有合适类型提醒,退出提醒线程 | threadId=%d", getId()));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 启动电量通知 TTS 语音提醒线程
|
||||||
|
TTSRemindThread.start(this.mContext, App.sQuantityOfElectricity, isCharging);
|
||||||
|
|
||||||
// 安全休眠,保留中断标记
|
// 安全休眠,保留中断标记
|
||||||
safeSleepInternal(sleepTime);
|
safeSleepInternal(sleepTime);
|
||||||
@@ -280,6 +283,7 @@ public class RemindThread extends Thread {
|
|||||||
*/
|
*/
|
||||||
private void cleanThreadStateInternal() {
|
private void cleanThreadStateInternal() {
|
||||||
LogUtils.d(TAG, String.format("cleanThreadStateInternal() 调用 | threadId=%d", getId()));
|
LogUtils.d(TAG, String.format("cleanThreadStateInternal() 调用 | threadId=%d", getId()));
|
||||||
|
TTSRemindThread.stopTTS();
|
||||||
isReminding = false;
|
isReminding = false;
|
||||||
isExist = true;
|
isExist = true;
|
||||||
// 中断当前线程(如果存活)
|
// 中断当前线程(如果存活)
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package cc.winboll.studio.powerbell.threads;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import cc.winboll.studio.libappbase.ToastUtils;
|
||||||
|
import cc.winboll.studio.powerbell.models.ThoughtfulServiceBean;
|
||||||
|
import cc.winboll.studio.powerbell.services.TTSPlayService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
|
||||||
|
* @Date 2026/02/28 20:41
|
||||||
|
* @Describe TTS 语音通知线程(单例 + 继承 Thread)
|
||||||
|
*/
|
||||||
|
public class TTSRemindThread extends Thread {
|
||||||
|
|
||||||
|
public static final String TAG = "TTSRemindThread";
|
||||||
|
|
||||||
|
// 单例实例
|
||||||
|
private static TTSRemindThread sInstance;
|
||||||
|
|
||||||
|
// 运行标志
|
||||||
|
private volatile boolean mIsRunning = false;
|
||||||
|
private volatile int mBattery;
|
||||||
|
private volatile boolean mIsCharging;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私有构造,单例禁用外部 new
|
||||||
|
*/
|
||||||
|
private TTSRemindThread(Context context) {
|
||||||
|
this.mContext = context.getApplicationContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单例(DCL 双重校验)
|
||||||
|
*/
|
||||||
|
public static TTSRemindThread getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
synchronized (TTSRemindThread.class) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new TTSRemindThread(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对外静态启动入口
|
||||||
|
*/
|
||||||
|
public static void start(Context context, int battery, boolean isCharging) {
|
||||||
|
TTSRemindThread instance = getInstance(context);
|
||||||
|
instance.mBattery = battery;
|
||||||
|
instance.mIsCharging = isCharging;
|
||||||
|
if (!instance.mIsRunning) {
|
||||||
|
LogUtils.d(TAG, "start() TTS 提醒线程启动");
|
||||||
|
instance.mIsRunning = true;
|
||||||
|
instance.start(); // 启动线程
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对外静态停止入口
|
||||||
|
*/
|
||||||
|
public static void stopTTS() {
|
||||||
|
if (sInstance != null) {
|
||||||
|
LogUtils.d(TAG, "stopTTS() TTS 提醒线程停止");
|
||||||
|
sInstance.mIsRunning = false;
|
||||||
|
sInstance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线程主逻辑
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
super.run();
|
||||||
|
LogUtils.d(TAG, "run() TTS 线程已开始循环");
|
||||||
|
|
||||||
|
while (mIsRunning) {
|
||||||
|
try {
|
||||||
|
// ======================
|
||||||
|
// 在这里写你的循环 TTS 逻辑
|
||||||
|
// ======================
|
||||||
|
|
||||||
|
// TTS 语音通知模块
|
||||||
|
// 读取 TTS 语音通知配置
|
||||||
|
ThoughtfulServiceBean ttsBean = ThoughtfulServiceBean.loadBean(mContext, ThoughtfulServiceBean.class);
|
||||||
|
if (ttsBean == null) {
|
||||||
|
ttsBean = new ThoughtfulServiceBean();
|
||||||
|
}
|
||||||
|
if (ttsBean.isEnableTtsWhenNotifyBattery()) {
|
||||||
|
//ToastUtils.show("Test");
|
||||||
|
// 启动
|
||||||
|
ToastUtils.show(String.format("mIsCharging %s, mBattery %d", mIsCharging, mBattery));
|
||||||
|
String text = mIsCharging ?"充电": "用电";
|
||||||
|
text += String.format("已达预定值百分之%d", mBattery);
|
||||||
|
TTSPlayService.startPlayTTS(mContext, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 防止死循环疯狂跑,加一点休眠
|
||||||
|
sleep(5000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LogUtils.e(TAG, "TTS 线程被中断", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mIsRunning = false;
|
||||||
|
LogUtils.d(TAG, "run() TTS 线程已退出");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否正在运行
|
||||||
|
*/
|
||||||
|
public boolean isRunning() {
|
||||||
|
return mIsRunning;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user