更新iat语音

This commit is contained in:
yaooo 2023-10-11 10:58:36 +08:00
parent 49e0be2e73
commit 87f2f22a32

View File

@ -139,18 +139,28 @@ class XunFeiController extends BaseApiController
if (!file_exists($file)) { if (!file_exists($file)) {
return $this->fail('未上传音频文件'); return $this->fail('未上传音频文件');
} }
$filesize = filesize($file);
if ($filesize > 1 * 1024 * 1024) {
return $this->fail('录音文件太长');
}
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext != 'mp3') {
return $this->fail('录音格式错误');
}
// $file = "https://lihai001.oss-cn-chengdu.aliyuncs.com/media/iat_mp3_16k.mp3"; // $file = "https://lihai001.oss-cn-chengdu.aliyuncs.com/media/iat_mp3_16k.mp3";
$audioFile = fopen($file, 'rb'); $audioFile = fopen($file, 'rb');
if ($audioFile === false) { if ($audioFile === false) {
return $this->fail('音频文件异常'); return $this->fail('音频文件异常');
} }
try {
$words = ''; $words = '';
$iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat"; $iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat";
$iat = new IatClient($this->app_id, $this->api_key, $this->api_secret); $iat = new IatClient($this->app_id, $this->api_key, $this->api_secret);
$client = new Client($iat->assembleAuthUrl($iatHostUrl)); $client = new Client($iat->assembleAuthUrl($iatHostUrl));
if ($client) { if ($client) {
$frameSize = 1280; //每一帧的音频大小 $frameSize = 1280; //每一帧的音频大小
$intervel = 20 * 1000; //发送音频间隔 $intervel = 40 * 1000; //发送音频间隔
$status = 0; $status = 0;
while (true) { while (true) {
$len = fread($audioFile, $frameSize); $len = fread($audioFile, $frameSize);
@ -237,6 +247,13 @@ class XunFeiController extends BaseApiController
if (file_exists($file)) { if (file_exists($file)) {
//unlink($file); //unlink($file);
} }
} catch (\Exception $e) {
if (file_exists($file)) {
// 删除临时文件
unlink($file);
}
return $this->fail($e->getMessage());
}
return $this->data(['words' => $words]); return $this->data(['words' => $words]);
} }