killapp/Assets/Scripts/UI/Pages/DeviceInfoPage/VideoAndSoundSetting.cs
2026-05-18 08:42:33 +08:00

303 lines
9.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using Kill.Bluetooth;
using Kill.Bluetooth.Protocol;
using Kill.Core;
using Kill.Managers;
using Kill.UI.Components;
using UnityEngine.UI;
using UnityEngine;
using System;
using Unity.VisualScripting;
namespace Kill.UI.Pages
{
public class VideoAndSoundSetting : MonoBehaviour
{
public Switch videoSwitch;
public Switch soundSwitch;
public MutiSelectPage videoLengthSelect;
public MutiSelectPage soundEffectSelect;
public Slider volumeSlider;
public Text videoLengthValueText;
public Text soundEffectValueText;
public Text volumeValueText;
// 视频时长选项3秒、5秒、10秒
private List<string> videoLengthKeyList = new List<string> { "100180", "100181", "100182" };
private List<string> videoLengthStrList = new List<string>();
private List<byte> videoLengthValueList = new List<byte> { 0x03, 0x05, 0x0A };
// 音效类型选项
private List<string> soundEffectKeyList = new List<string> { "100183", "100184", "100185", "100186", "100187" };
private List<string> soundEffectStrList = new List<string>();
private List<SoundEffectType> soundEffectValueList = new List<SoundEffectType>
{
SoundEffectType.Hello,
SoundEffectType.Goodbye,
SoundEffectType.Test,
SoundEffectType.BIU,
SoundEffectType.Main
};
private MultimediaControl multimediaSetting;
private Action callback;
public void Init(Action callback)
{
this.callback = callback;
InitLanguageText();
ReadMultimediaSettings();
UIManager.Instance.RegisterBackAction(Back);
volumeSlider.onValueChanged.RemoveAllListeners();
volumeSlider.onValueChanged.AddListener(OnVolumeChanged);
}
/// <summary>
/// 初始化语言文本
/// </summary>
private void InitLanguageText()
{
videoLengthStrList.Clear();
foreach (var key in videoLengthKeyList)
{
videoLengthStrList.Add(LanguageManager.Instance.GetLanguage(key));
}
soundEffectStrList.Clear();
foreach (var key in soundEffectKeyList)
{
soundEffectStrList.Add(LanguageManager.Instance.GetLanguage(key));
}
}
/// <summary>
/// 读取多媒体设置
/// </summary>
private void ReadMultimediaSettings()
{
#if UNITY_EDITOR
// 编辑器模式下使用默认值
multimediaSetting = new MultimediaControl
{
VideoRecordEnable = true,
RecordDuration = 0x05,
SoundEnable = true,
SoundType = SoundEffectType.Hello,
Volume = 10
};
UpdateUI();
return;
#endif
LoadingUI.Show();
BLECommunicationManager.Instance.ReadMultimediaControl((setting) =>
{
Loom.QueueOnMainThread(() =>
{
multimediaSetting = setting;
UpdateUI();
});
});
}
/// <summary>
/// 写入多媒体设置
/// </summary>
private void WriteMultimediaSetting()
{
#if UNITY_EDITOR
UpdateUI();
return;
#endif
LoadingUI.Show();
BLECommunicationManager.Instance.WriteMultimediaControl(multimediaSetting, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
UpdateUI();
}
else
{
ReadMultimediaSettings();
}
});
});
}
/// <summary>
/// 更新UI显示
/// </summary>
private void UpdateUI()
{
// 更新视频录制开关
videoSwitch.Init(multimediaSetting.VideoRecordEnable, OnVideoSwitchChanged);
// 更新音效开关
soundSwitch.Init(multimediaSetting.SoundEnable, OnSoundSwitchChanged);
// 更新视频时长显示
int videoLengthIndex = GetVideoLengthIndexFromValue(multimediaSetting.RecordDuration);
if (videoLengthValueText != null && videoLengthIndex >= 0 && videoLengthIndex < videoLengthStrList.Count)
{
videoLengthValueText.text = videoLengthStrList[videoLengthIndex];
}
// 更新音效类型显示
int soundEffectIndex = GetSoundEffectIndexFromValue(multimediaSetting.SoundType);
if (soundEffectValueText != null && soundEffectIndex >= 0 && soundEffectIndex < soundEffectStrList.Count)
{
soundEffectValueText.text = soundEffectStrList[soundEffectIndex];
}
// 更新音量滑块
volumeSlider.value = multimediaSetting.Volume;
volumeSlider.onValueChanged.RemoveAllListeners();
volumeSlider.onValueChanged.AddListener(OnVolumeChanged);
if (volumeValueText != null)
{
volumeValueText.text = $"{multimediaSetting.Volume}";
}
LoadingUI.Hide();
UIManager.Instance.RegisterBackAction(Back);
}
/// <summary>
/// 视频录制开关变化
/// </summary>
private void OnVideoSwitchChanged(bool isOn)
{
multimediaSetting.VideoRecordEnable = isOn;
WriteMultimediaSetting();
}
/// <summary>
/// 音效开关变化
/// </summary>
private void OnSoundSwitchChanged(bool isOn)
{
multimediaSetting.SoundEnable = isOn;
WriteMultimediaSetting();
}
Coroutine volumeChangeCoroutine;
/// <summary>
/// 音量滑块变化
/// </summary>
private void OnVolumeChanged(float value)
{
byte volume = (byte)Mathf.Clamp(value, 0, 15);
multimediaSetting.Volume = volume;
if (volumeValueText != null)
{
volumeValueText.text = $"{volume}";
}
if (volumeChangeCoroutine != null)
{
StopCoroutine(volumeChangeCoroutine);
}
volumeChangeCoroutine = StartCoroutine(CountDownAndWriteMultimediaSetting(0.5f));
}
private IEnumerator CountDownAndWriteMultimediaSetting(float delay)
{
yield return new WaitForSeconds(delay);
WriteMultimediaSetting();
}
/// <summary>
/// 音量滑块拖动结束
/// </summary>
public void OnVolumeSliderEndDrag()
{
WriteMultimediaSetting();
}
/// <summary>
/// 点击视频时长设置
/// </summary>
public void ClickVideoLength()
{
MutiSelectPage m = Instantiate(videoLengthSelect.gameObject, transform).GetComponent<MutiSelectPage>();
int currentIndex = GetVideoLengthIndexFromValue(multimediaSetting.RecordDuration);
m.Init(currentIndex, OnVideoLengthSelected);
m.gameObject.SetActive(true);
UIManager.Instance.RegisterBackAction(m.Cancel);
}
/// <summary>
/// 视频时长选择回调
/// </summary>
private void OnVideoLengthSelected(int index)
{
if (index >= 0 && index < videoLengthValueList.Count)
{
multimediaSetting.RecordDuration = videoLengthValueList[index];
WriteMultimediaSetting();
}
}
/// <summary>
/// 点击音效类型设置
/// </summary>
public void ClickSoundEffect()
{
MutiSelectPage m = Instantiate(soundEffectSelect.gameObject, transform).GetComponent<MutiSelectPage>();
int currentIndex = GetSoundEffectIndexFromValue(multimediaSetting.SoundType);
m.Init(currentIndex, OnSoundEffectSelected);
m.gameObject.SetActive(true);
UIManager.Instance.RegisterBackAction(m.Cancel);
}
/// <summary>
/// 音效类型选择回调
/// </summary>
private void OnSoundEffectSelected(int index)
{
if (index >= 0 && index < soundEffectValueList.Count)
{
multimediaSetting.SoundType = soundEffectValueList[index];
WriteMultimediaSetting();
}
}
/// <summary>
/// 根据视频时长值获取选项索引
/// </summary>
private int GetVideoLengthIndexFromValue(byte value)
{
for (int i = 0; i < videoLengthValueList.Count; i++)
{
if (videoLengthValueList[i] == value)
{
return i;
}
}
// 默认值返回5秒(索引1)
return 1;
}
/// <summary>
/// 根据音效类型值获取选项索引
/// </summary>
private int GetSoundEffectIndexFromValue(SoundEffectType value)
{
for (int i = 0; i < soundEffectValueList.Count; i++)
{
if (soundEffectValueList[i] == value)
{
return i;
}
}
// 默认值返回Hello(索引0)
return 0;
}
public void Back()
{
callback?.Invoke();
Destroy(gameObject);
}
}
}