diff --git a/lib/data/models/appconfig.dart b/lib/data/models/appconfig.dart index e80862486..952746321 100644 --- a/lib/data/models/appconfig.dart +++ b/lib/data/models/appconfig.dart @@ -4,7 +4,6 @@ import 'package:deep_voice/data/models/appconfig_model.dart'; class AppConfig { static late UserGetAppConfigResp config; - // 可选:提供初始化方法 static void initialize(UserGetAppConfigResp resp) { config = resp; @@ -25,4 +24,10 @@ class AppConfig { final foundAgent = config.agents.firstWhere((agent) => agent.id == agentId); return foundAgent.systemPrompt; } + + static List allproductNames() { + List names = + config.products.map((user) => user.devicename).toList(); + return names; + } } diff --git a/lib/data/models/appconfig_model.dart b/lib/data/models/appconfig_model.dart index 602abf4dd..dfbbd8c70 100644 --- a/lib/data/models/appconfig_model.dart +++ b/lib/data/models/appconfig_model.dart @@ -7,8 +7,13 @@ class UserGetAppConfigResp { final Map env; final List agents; final Map mcps; + final List products; + UserGetAppConfigResp( - {required this.env, required this.agents, required this.mcps}); + {required this.env, + required this.agents, + required this.mcps, + required this.products}); factory UserGetAppConfigResp.fromJson(Map json) => _$UserGetAppConfigRespFromJson(json); @@ -63,3 +68,20 @@ class DBMCPServer { _$DBMCPServerFromJson(json); Map toJson() => _$DBMCPServerToJson(this); } + +@JsonSerializable() +class DBProduct { + final String id; + final int factoryid; + final String devicename; + + DBProduct({ + required this.id, + required this.factoryid, + required this.devicename, + }); + + factory DBProduct.fromJson(Map json) => + _$DBProductFromJson(json); + Map toJson() => _$DBProductToJson(this); +} diff --git a/lib/data/models/appconfig_model.g.dart b/lib/data/models/appconfig_model.g.dart index c4ac4cc90..3c6675929 100644 --- a/lib/data/models/appconfig_model.g.dart +++ b/lib/data/models/appconfig_model.g.dart @@ -16,6 +16,9 @@ UserGetAppConfigResp _$UserGetAppConfigRespFromJson( mcps: (json['mcps'] as Map).map( (k, e) => MapEntry(k, DBMCPServer.fromJson(e as Map)), ), + products: (json['products'] as List) + .map((e) => DBProduct.fromJson(e as Map)) + .toList(), ); Map _$UserGetAppConfigRespToJson( @@ -24,6 +27,7 @@ Map _$UserGetAppConfigRespToJson( 'env': instance.env, 'agents': instance.agents, 'mcps': instance.mcps, + 'products': instance.products, }; DBAgent _$DBAgentFromJson(Map json) => DBAgent( @@ -66,3 +70,15 @@ Map _$DBMCPServerToJson(DBMCPServer instance) => 'url': instance.url, 'tools': instance.tools, }; + +DBProduct _$DBProductFromJson(Map json) => DBProduct( + id: json['id'] as String, + factoryid: (json['factoryid'] as num).toInt(), + devicename: json['devicename'] as String, + ); + +Map _$DBProductToJson(DBProduct instance) => { + 'id': instance.id, + 'factoryid': instance.factoryid, + 'devicename': instance.devicename, + }; diff --git a/lib/data/models/user_Info.dart b/lib/data/models/user_Info.dart index c5be98057..3f3f9fa86 100644 --- a/lib/data/models/user_Info.dart +++ b/lib/data/models/user_Info.dart @@ -23,6 +23,7 @@ class User { int viplv = 0; //添加会员等级字段,0表示普通用户 String earphonename; String earphonemac; + String earphonelicense; String earphoneauthcode; // final Int64 lastbettime; // final Int64 createtime; @@ -44,6 +45,7 @@ class User { required this.viplv, // 添加默认值 required this.earphonename, required this.earphonemac, + required this.earphonelicense, required this.earphoneauthcode, // required this.lastbettime, // required this.createtime, @@ -74,6 +76,7 @@ class User { viplv: (userData?['viplv'] as int?) ?? 0, // 提供默认值 earphonename: (userData?['earphonename'] as String?) ?? '', earphonemac: (userData?['earphonemac'] as String?) ?? '', + earphonelicense: (userData?['earphonelicense'] as String?) ?? '', earphoneauthcode: (userData?['earphoneauthcode'] as String?) ?? '', // lastbettime: _parseTimestamp(userData['lastbettime']), // createtime: _parseTimestamp(userData['createtime']), @@ -102,6 +105,7 @@ class User { viplv: (userData?['viplv'] as int?) ?? 0, // 提供默认值 earphonename: (userData?['earphonename'] as String?) ?? '', earphonemac: (userData?['earphonemac'] as String?) ?? '', + earphonelicense: (userData?['earphonelicense'] as String?) ?? '', earphoneauthcode: (userData?['earphoneauthcode'] as String?) ?? '', // lastbettime: _parseTimestamp(userData['lastbettime']), // createtime: _parseTimestamp(userData['createtime']), @@ -128,6 +132,7 @@ class User { 'earphonename': earphonename, 'earphonemac': earphonemac, + "earphonelicense": earphonelicense, 'earphoneauthcode': earphoneauthcode, // 'lastbettime': lastbettime, diff --git a/lib/data/services/network/api.dart b/lib/data/services/network/api.dart index 0c7235c3b..c44f1b391 100644 --- a/lib/data/services/network/api.dart +++ b/lib/data/services/network/api.dart @@ -74,10 +74,19 @@ class Api { } //app获取设备信息 - static getequipmentinfo([params]) { + static getproduct([params]) { return DioManager().request( NWMethod.post, - '/api/home/user_getequipmentinfo', + '/api/home/user_getproduct', + params: params, + ); + } + + //app获取设备信息 + static getproducts([params]) { + return DioManager().request( + NWMethod.post, + '/api/home/user_getproducts', params: params, ); } diff --git a/lib/data/services/network/nw_api.dart b/lib/data/services/network/nw_api.dart index c3b6a2f8b..8feff1db5 100644 --- a/lib/data/services/network/nw_api.dart +++ b/lib/data/services/network/nw_api.dart @@ -1,5 +1,5 @@ class NWApi { // static const baseApi = "https://www.deapsound.com"; - //static const baseApi = "https://api.deapsound.com"; - static const baseApi = "https://api.ideapsound.com"; + static const baseApi = "https://api.deapsound.com"; + // static const baseApi = "https://api.ideapsound.com"; } diff --git a/lib/modules/devices/controllers/devices_controller.dart b/lib/modules/devices/controllers/devices_controller.dart index 2ca2e8f47..416918206 100644 --- a/lib/modules/devices/controllers/devices_controller.dart +++ b/lib/modules/devices/controllers/devices_controller.dart @@ -13,12 +13,14 @@ import 'package:get/get_state_manager/src/simple/get_controllers.dart'; class DevicesController extends GetxController { late final BleManager _bleManager; //耳机绑定 - final isearphone = false.obs; + final earphonestate = 0.obs; final earphoneName = "".obs; final earphoneMac = "".obs; + final earphoneLicense = "".obs; final earphoneCode = "".obs; //录音笔 + final voicepenstate = 0.obs; final isvoicepen = false.obs; final voicepenName = "".obs; final voicepenMac = "".obs; @@ -29,9 +31,13 @@ class DevicesController extends GetxController { _bleManager = Get.find(); earphoneName.value = User.instance.earphonename; earphoneMac.value = User.instance.earphonemac; + earphoneLicense.value = User.instance.earphonelicense; earphoneCode.value = User.instance.earphoneauthcode; if (earphoneCode.value != "") { - isearphone.value = true; + earphonestate.value = 1; + } + if (earphoneLicense.value != "") { + earphonestate.value = 2; } } @@ -48,8 +54,8 @@ class DevicesController extends GetxController { if (response != null) { User.instance.earphonename = ""; User.instance.earphonemac = ""; - User.instance.earphoneauthcode = ""; - isearphone.value = false; + User.instance.earphonelicense = ""; + earphonestate.value = 0; _bleManager.clearPairedDevices(); _bleManager.disconnect(); Get.toNamed(Routes.pairing); diff --git a/lib/modules/devices/views/devices_view.dart b/lib/modules/devices/views/devices_view.dart index d77b08253..d01198e08 100644 --- a/lib/modules/devices/views/devices_view.dart +++ b/lib/modules/devices/views/devices_view.dart @@ -93,10 +93,10 @@ class DevicesView extends GetView { () => // 耳机列表 _buildSimpleNavigationSetting( - title: controller.isearphone.value + title: controller.earphonestate.value != 0 ? controller.earphoneName.value : "未绑定", - subtitle: controller.isearphone.value + subtitle: controller.earphonestate.value != 0 ? controller.earphoneMac.value : "还未绑定设备", icon: Icons.headphones, @@ -110,7 +110,7 @@ class DevicesView extends GetView { titleColor: isDarkMode ? Colors.white : null, subtitleColor: isDarkMode ? Colors.white70 : null, isDarkMode: isDarkMode, - showbutt: controller.isearphone.value, + showbutt: controller.earphonestate == 2, onPressed: () { controller.unbindearphone(); }, diff --git a/lib/modules/login/controllers/login_controller.dart b/lib/modules/login/controllers/login_controller.dart index a06a2cd1f..c96439363 100644 --- a/lib/modules/login/controllers/login_controller.dart +++ b/lib/modules/login/controllers/login_controller.dart @@ -11,7 +11,6 @@ import 'weixinLogin.dart'; import '../../../data/services/network/api.dart'; import '../../../../data/models/user_Info.dart'; import '../../common/webview_page.dart'; -//import 'package:shared_preferences/shared_preferences.dart'; import 'package:get_storage/get_storage.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'dart:io'; @@ -914,10 +913,6 @@ class LoginController extends GetxController { print('getAppConfig:进来了'); // 根据API设计调整 final resp = UserGetAppConfigResp.fromJson(response); - // 存储用户信息(示例) - // _storage.write('ENV', resp.env); - // _storage.write('AGENTS', resp.agents); - // _storage.write('MCPS', resp.mcps); AppConfig.initialize(resp); return true; } catch (e) { diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index e87191f33..71f9f98fd 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -184,7 +184,7 @@ class OtaUpgradeController extends GetxController { jlota.init(); // API请求 - final response = await Api.getequipmentinfo({ + final response = await Api.getproduct({ "id": deviceInfo.firmwareProductId, }); diff --git a/lib/modules/pairing/controllers/pairing_controller.dart b/lib/modules/pairing/controllers/pairing_controller.dart index 2bfcfcedb..20eb21cdf 100644 --- a/lib/modules/pairing/controllers/pairing_controller.dart +++ b/lib/modules/pairing/controllers/pairing_controller.dart @@ -431,9 +431,12 @@ class PairingController extends GetxController { try { Logger.i(_tag, '开始验证设备: ${device.name} (${device.address})'); final result = await authbindingDevice(device); - if (result == 2) { + if (result == 10) { //验证失败 进入手动授权 await connectble(device); + } else if (result == 9) { + Logger.i(_tag, 'BLE已连接,直接完成配对'); + _transition(PairingState.completed); } else if (result == 1) { //跳转绑定界面 //验证失败 进入手动授权 @@ -557,15 +560,22 @@ class PairingController extends GetxController { //自动绑定设备 0 未合作商 1 合作商 2,已绑定 Future authbindingDevice(BluetoothDevice device) async { - if (User.instance.earphonemac == device.address) { + if (User.instance.earphonelicense != "" && + User.instance.earphonemac == device.address) { // 已经绑定 - return 2; + return 10; + } + if (User.instance.earphoneauthcode != "" && + User.instance.earphonemac == device.address) { + // 已经绑定 + return 9; } - // 检查设备名称是否包含 DeepSound 或 DeapSound final deviceName = device.name; - print('获取到 deviceName: $deviceName'); - if (deviceName.contains('DeepSound') || deviceName.contains('DeapSound')) { + + final names = AppConfig.allproductNames(); + print('获取到 deviceName: $deviceName names:$names'); + if (names.contains(deviceName)) { await _bleManager.startScan(deviceName); await Future.delayed(const Duration(seconds: 1)); // 尝试最多读取 3 次设备信息,每次间隔 1 秒 @@ -610,7 +620,7 @@ class PairingController extends GetxController { Logger.i(_tag, '绑定设备成功'); User.instance.earphonemac = currentDevice?.address ?? ''; User.instance.earphonename = currentDevice?.name ?? ''; - User.instance.earphoneauthcode = deviceCode.value; + User.instance.earphonelicense = deviceCode.value; _transition(PairingState.verifying); _verifyDevice(_currentClassicDevice!); return; @@ -642,8 +652,9 @@ class PairingController extends GetxController { void onAutoCodeInputTap() async { // 调用BLE管理器的自动绑定方法 final response = await Api.bindauthcode({ + "code": codeInputController.text.trim(), + "earphonname": currentDevice?.name, "earphonemac": currentDevice?.address, - "earphoneauthcode": codeInputController.text.trim(), }); Logger.i(_tag, '手动授权结果 ${response}'); if (response != null) { diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt index 01b42c684..ec5777305 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt @@ -734,6 +734,7 @@ object AgentService : CoroutineScope { ) { Log.d(TAG, "内部方法:通用的ChatAPI处理逻辑") // 如果有正在进行的AI流式输出,先停止它 + audioPlayer?.stopAudio() stopAiStream() stopTts() // 记录新的AI任务 @@ -748,6 +749,7 @@ object AgentService : CoroutineScope { val responseBuilder = StringBuilder() var aiMetadata:String = "" var userMetadata = JSONObject() + var reply = false; //是否回复 var broadcast = true //是否播发 // 如果有图片,添加图片信息到元数据 if (hasImage) { @@ -814,10 +816,10 @@ object AgentService : CoroutineScope { return } responseBuilder.append(token) - if (speakResponse && broadcast) { + if (speakResponse && reply && broadcast) { ttsService?.speakStream(token) } - if (broadcast) { + if (reply && token.isNotEmpty()) { // 发送流式回复token sendEvent("assistant_token", mapOf("token" to token)) } @@ -829,7 +831,7 @@ object AgentService : CoroutineScope { override fun onComplete(sessionid:String) { try { // 视情况决定是否朗读回复 - if (speakResponse && broadcast && sessionid == currsessionId){ + if (speakResponse && reply && broadcast && sessionid == currsessionId){ ttsService?.flushStream() audioPlayer?.stopAudio() } @@ -842,7 +844,10 @@ object AgentService : CoroutineScope { if (hasImage) { responseData["hasImage"] = true } - sendEvent("assistant_response", responseData) + if (reply){ + sendEvent("assistant_response", responseData) + } + if (response.isNotEmpty()) { // 添加AI回复到历史记录 addToHistoryMessages(createAssistantMessage(response)) @@ -903,6 +908,7 @@ object AgentService : CoroutineScope { val resultmap = autoHandleFcunCallResult(name, functionCallResult); aiMetadata = resultmap["meta"] as String; broadcast = resultmap["broadcast"] as Boolean; + reply = !(resultmap["notreply"] as Boolean); sendEvent( "function_call_result", mapOf( "function_call" to functionCall.toString(), diff --git a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt index 24f56af0d..984b48475 100644 --- a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt +++ b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt @@ -8,6 +8,7 @@ import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.location.LocationManager +import android.media.AudioManager import android.net.Uri import android.os.Build import android.provider.AlarmClock @@ -232,6 +233,42 @@ class SystemFunctionHandler(private val context: Context? = null) { ), handler = ExitNavigationHandler(context) ) + + + // 增加音量 + client.registerLocalFunction( + name = "add_volume", + description = "增大音量", + parameters = mapOf( + "type" to "object", + "properties" to emptyMap(), + "required" to emptyList() + ), + handler = AddVolumeHandler(context) + ) + + // 增加音量 + client.registerLocalFunction( + name = "reduce_volume", + description = "降低音量", + parameters = mapOf( + "type" to "object", + "properties" to emptyMap(), + "required" to emptyList() + ), + handler = ReduceVolumeHandler(context) + ) + // 设置静音 + client.registerLocalFunction( + name = "mute_volume", + description = "设置静音", + parameters = mapOf( + "type" to "object", + "properties" to emptyMap(), + "required" to emptyList() + ), + handler = MuteVolumeHandler(context) + ) } /** @@ -1320,4 +1357,109 @@ private class OpenRecorderHandler(private val context: Context?) : FunctionHandl }.toString() } } -} \ No newline at end of file +} + +/** + * 添加音量处理类 + */ +private class AddVolumeHandler(private val context: Context?) : FunctionHandler { + + //设置音量 + fun setVolume(context: Context, streamType: Int, volume: Int) { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + val maxVolume = audioManager.getStreamMaxVolume(streamType) + val safeVolume = volume.coerceIn(0, maxVolume) + audioManager.setStreamVolume(streamType, safeVolume, AudioManager.FLAG_SHOW_UI) + } + fun getCurrentVolume(context: Context, streamType: Int): Int { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + return audioManager.getStreamVolume(streamType) + } + fun getMaxVolume(context: Context, streamType: Int): Int { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + return audioManager.getStreamMaxVolume(streamType) + } + //增加音量/减少音量 + fun adjustVolume(context: Context, direction: Int) { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, direction, AudioManager.FLAG_SHOW_UI) + } + + override suspend fun handle(arguments: Map): String { + if (context == null) { + return JSONObject().apply { + put("error", JSONObject().apply { + put("code", "context_error") + put("message", "上下文未初始化") + }) + }.toString() + } + + // 发送广播通知退出交互 + val result = adjustVolume(context, AudioManager.ADJUST_RAISE) + + return JSONObject().apply { + put("result", "增加音量成功!") + put("broadcast", false) + }.toString() + } +} +/** + * 添加音量处理类 + */ +private class ReduceVolumeHandler(private val context: Context?) : FunctionHandler { + //增加音量/减少音量 + fun adjustVolume(context: Context, direction: Int) { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, direction, AudioManager.FLAG_SHOW_UI) + } + + override suspend fun handle(arguments: Map): String { + if (context == null) { + return JSONObject().apply { + put("error", JSONObject().apply { + put("code", "context_error") + put("message", "上下文未初始化") + }) + }.toString() + } + + // 发送广播通知退出交互 + adjustVolume(context, AudioManager.ADJUST_LOWER) + return JSONObject().apply { + put("result", "降低音量成功!") + put("broadcast", false) + }.toString() + } +} +/** + * 添加音量处理类 + */ +private class MuteVolumeHandler(private val context: Context?) : FunctionHandler { + + //设置音量 + fun setVolume(context: Context, streamType: Int, volume: Int) { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + val maxVolume = audioManager.getStreamMaxVolume(streamType) + val safeVolume = volume.coerceIn(0, maxVolume) + audioManager.setStreamVolume(streamType, safeVolume, AudioManager.FLAG_SHOW_UI) + } + + override suspend fun handle(arguments: Map): String { + if (context == null) { + return JSONObject().apply { + put("error", JSONObject().apply { + put("code", "context_error") + put("message", "上下文未初始化") + }) + }.toString() + } + + // 发送广播通知退出交互 + setVolume(context, AudioManager.STREAM_MUSIC, 0) + return JSONObject().apply { + put("result", "设置静音成功!") + put("broadcast", false) + }.toString() + } +} diff --git a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/SystemFunctionHandler.kt b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/SystemFunctionHandler.kt index 0dbe86e5a..643b31688 100644 --- a/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/SystemFunctionHandler.kt +++ b/local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/SystemFunctionHandler.kt @@ -30,6 +30,7 @@ import androidx.core.content.ContextCompat import android.app.ActivityManager import android.app.Application import android.content.ComponentName +import android.media.AudioManager import android.os.Process /** * 系统功能处理器 - 负责调用系统API执行各种功能以及注册系统功能 @@ -78,6 +79,13 @@ class SystemFunctionHandler(private val context: Context) { // 注册关闭导航服务功能 registerStopNavigationFunction(client) + + //增加音量 + registerAddVolumeFunction(client) + //减少音量 + registerReduceVolumeFunction(client) + //静音 + registerMuteVolumeFunction(client) Log.d(TAG, "所有系统功能注册完成") } catch (e: Exception) { Log.e(TAG, "注册系统功能失败: ${e.message}", e) @@ -438,6 +446,78 @@ class SystemFunctionHandler(private val context: Context) { ) Log.d(TAG, "已注册获取当前时间功能") } + /** + * 注册添加音量函数 + */ + private fun registerAddVolumeFunction(client: MCPClient) { + client.registerLocalFunction( + "add_volume", + "增大音量", + JSONObject(""" + { + "type": "object", + "properties": {}, + "required": [] + } + """), + object : FunctionHandler { + override suspend fun handle(arguments: Map): String { + val result = adjustVolume(context, AudioManager.ADJUST_RAISE) + return "{\"result\": \"增加音量成功!\"}" + } + } + ) + Log.d(TAG, "已注册音量调节功能") + } + + /** + * 注册减少音量函数 + */ + private fun registerReduceVolumeFunction(client: MCPClient) { + client.registerLocalFunction( + "reduce_volume", + "减少音量", + JSONObject(""" + { + "type": "object", + "properties": {}, + "required": [] + } + """), + object : FunctionHandler { + override suspend fun handle(arguments: Map): String { + val result = adjustVolume(context, AudioManager.ADJUST_LOWER) + return "{\"result\": \"减少音量成功!\"}" + } + } + ) + Log.d(TAG, "已注册音量调节功能") + } + + /** + * 注册静音函数 + */ + private fun registerMuteVolumeFunction(client: MCPClient) { + client.registerLocalFunction( + "mute_volume", + "静音设置", + JSONObject(""" + { + "type": "object", + "properties": {}, + "required": [] + } + """), + object : FunctionHandler { + override suspend fun handle(arguments: Map): String { + val result = setVolume(context, AudioManager.STREAM_MUSIC, 0) + return "{\"result\": \"减少音量成功!\"}" + } + } + ) + Log.d(TAG, "已注册音量调节功能") + } + /** * 控制媒体播放 * @param action 操作类型:"play", "pause", "previous", "next" @@ -1241,6 +1321,28 @@ class SystemFunctionHandler(private val context: Context) { } } + + //设置音量 + fun setVolume(context: Context, streamType: Int, volume: Int) { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + val maxVolume = audioManager.getStreamMaxVolume(streamType) + val safeVolume = volume.coerceIn(0, maxVolume) + audioManager.setStreamVolume(streamType, safeVolume, AudioManager.FLAG_SHOW_UI) + } + fun getCurrentVolume(context: Context, streamType: Int): Int { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + return audioManager.getStreamVolume(streamType) + } + fun getMaxVolume(context: Context, streamType: Int): Int { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + return audioManager.getStreamMaxVolume(streamType) + } + //增加音量/减少音量 + fun adjustVolume(context: Context, direction: Int) { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, direction, AudioManager.FLAG_SHOW_UI) + } + /** * 操作结果数据类 */