pico neo3,unity 2021开发,驰声语音测评unity SDK,用户射线点击UI面板录音按钮对单词录音,自动检测录音状态,同步显示录音可视化,如波状图,录音结束显示测评分数UI,回放录音,重录功能。对应脚本和unity事件的绑定、驰声sdk与unity功能的接入、申请驰声API接口的步骤。请分别给出具体详细步骤和对应脚本C#的完整实例。
时间: 2023-08-04 07:03:50 浏览: 144
Pico开发工具包-Pico Unity Integration SDK-2.0.5
步骤如下:
1. 下载驰声语音测评unity SDK,将SDK导入Unity项目中
2. 在Unity中创建一个UI面板,添加一个录音按钮和波状图的UI元素
3. 创建一个名为RecordButton的脚本,并将其绑定到录音按钮的onClick事件上
```csharp
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
using AILoudSpeaker;
public class RecordButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public Image waveImage; //波状图UI元素
public Text scoreText; //显示分数的UI元素
private bool isRecording = false; //是否正在录音
private string recordFilePath; //录音文件保存路径
private float[] waveData; //波形数据
private float recordTime = 0.0f; //录音时间
public void OnPointerDown(PointerEventData eventData)
{
StartRecording();
}
public void OnPointerUp(PointerEventData eventData)
{
StopRecording();
}
//开始录音
private void StartRecording()
{
if (!isRecording)
{
isRecording = true;
recordFilePath = Application.persistentDataPath + "/record.wav";
ASREngine.GetInstance().StartRecord(recordFilePath); //调用驰声SDK的录音接口
}
}
//停止录音
private void StopRecording()
{
if (isRecording)
{
isRecording = false;
ASREngine.GetInstance().StopRecord(); //调用驰声SDK的停止录音接口
StartCoroutine(ShowScore()); //开始测评
}
}
//显示录音可视化波状图
private void ShowWaveImage()
{
waveImage.sprite = Sprite.Create(Texture2D.whiteTexture, new Rect(0, 0, 1, waveData.Length), new Vector2(0.5f, 0.5f));
waveImage.type = Image.Type.Filled;
waveImage.fillMethod = Image.FillMethod.Vertical;
waveImage.fillOrigin = (int)Image.OriginVertical.Bottom;
waveImage.fillAmount = 1.0f;
waveImage.color = Color.green;
}
//显示测评分数
private IEnumerator ShowScore()
{
yield return new WaitForSeconds(1.0f); //等待1s,确保录音文件已经生成
recordTime = ASREngine.GetInstance().GetRecordTime(recordFilePath); //获取录音时长
waveData = ASREngine.GetInstance().GetWaveData(recordFilePath); //获取录音的波形数据
ShowWaveImage(); //显示波状图
float score = ASREngine.GetInstance().GetScore(recordFilePath, recordTime); //调用驰声SDK的测评接口,获取测评分数
scoreText.text = "Score: " + score.ToString(); //显示分数
}
}
```
4. 在代码中调用驰声SDK的接口,需要先申请API接口,具体可以参照驰声官网的开发文档。在代码中调用驰声SDK的接口需要先初始化SDK,可以在Unity项目启动时进行初始化。
```csharp
using UnityEngine;
using System.Collections;
using AILoudSpeaker;
public class ASREngineManager : MonoBehaviour
{
public string appKey = "your app key";
public string secretKey = "your secret key";
void Awake()
{
ASREngine.GetInstance().Init(appKey, secretKey); //初始化驰声SDK
}
}
```
5. 在Unity中添加一个名为PlaybackButton的按钮,用于播放录音
```csharp
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlaybackButton : MonoBehaviour
{
public Button playButton;
public Button stopButton;
private AudioSource audioSource;
private AudioClip audioClip;
void Start()
{
playButton.onClick.AddListener(PlayRecord);
stopButton.onClick.AddListener(StopPlayback);
audioSource = gameObject.GetComponent<AudioSource>();
}
//播放录音
private void PlayRecord()
{
audioClip = Microphone.Start(null, false, 10, 16000); //从麦克风中获取录音数据
audioSource.clip = audioClip;
audioSource.Play();
}
//停止播放
private void StopPlayback()
{
audioSource.Stop();
Microphone.End(null);
}
}
```
6. 将RecordButton和PlaybackButton的脚本分别绑定到录音按钮和播放按钮的onClick事件上,即可完成录音和播放功能的实现。
以上就是使用驰声语音测评unity SDK实现录音、测评、播放功能的完整步骤和对应脚本C#的实例。
阅读全文