122 lines
2.9 KiB
C#
122 lines
2.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.IO;
|
|
|
|
public class UItest : MonoBehaviour
|
|
{
|
|
//语音转文字
|
|
public Text att_text;
|
|
public Button att_startbtn;
|
|
public Button att_stopbtn;
|
|
|
|
////文字转语音
|
|
public InputField tta_input;
|
|
public Button tta_transbtn;
|
|
public AudioSource tta_au;
|
|
|
|
public Text stateText;
|
|
|
|
//chat
|
|
public Text chat_ansewr;
|
|
public InputField ask;
|
|
public Dropdown uptextNum;
|
|
public Button sendBtn;
|
|
|
|
void Start()
|
|
{
|
|
Screen.SetResolution(1600, 900, FullScreenMode.Windowed);
|
|
|
|
att_startbtn.onClick.AddListener(MicrophoneInput.instance.OnStartClick);
|
|
att_stopbtn.onClick.AddListener(()=>
|
|
{
|
|
//停止录音,并转换成文字
|
|
MicrophoneInput.instance.OnStopClick((str)=>
|
|
{
|
|
att_text.text = str;
|
|
|
|
tta_input.text = str;
|
|
|
|
ToAudio();
|
|
});
|
|
});
|
|
|
|
tta_transbtn.onClick.AddListener(()=>
|
|
{
|
|
ToAudio();
|
|
});
|
|
|
|
sendBtn.onClick.AddListener(() =>
|
|
{
|
|
Chat();
|
|
});
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
stateText.text = MyDebugger.msg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转语音
|
|
/// </summary>
|
|
private void ToAudio()
|
|
{
|
|
if (tta_input.text.Length > 0)
|
|
{
|
|
//zhangheyi
|
|
/*
|
|
IFlytekManager.instance.TextToAudio(tta_input.text, clip =>
|
|
{
|
|
if (clip != null)
|
|
{
|
|
Debug.Log("ToAudio:开始播放");
|
|
tta_au.clip = clip;
|
|
tta_au.Play();
|
|
}
|
|
});
|
|
*/
|
|
IFlytekManager.instance.TextToAudio(tta_input.text);
|
|
}
|
|
}
|
|
|
|
|
|
List<ChatData> histryChat=new List<ChatData>();
|
|
private void Chat()
|
|
{
|
|
if(ask.text!="")
|
|
{
|
|
chat_ansewr.text = "思考中";
|
|
string askmsg=ask.text;
|
|
if (uptextNum.value == 0)
|
|
{
|
|
IFlytekManager.instance.Chat(askmsg, result =>
|
|
{
|
|
chat_ansewr.text = result;
|
|
ask.text = "";
|
|
histryChat.Add(new ChatData { ask = askmsg, answer = result });
|
|
});
|
|
}
|
|
else
|
|
{
|
|
//获取倒数第n个
|
|
List<ChatData> tmps=new List<ChatData>();
|
|
for (int i = histryChat.Count-1; i >= 0 && tmps.Count<= uptextNum.value; i--)
|
|
{
|
|
tmps.Add(histryChat[i]);
|
|
}
|
|
|
|
IFlytekManager.instance.Chat(tmps, askmsg, result =>
|
|
{
|
|
chat_ansewr.text = result;
|
|
ask.text = "";
|
|
histryChat.Add(new ChatData { ask = askmsg, answer = result });
|
|
});
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|