这次修改后台时,对切分好的plaintext请求科大讯飞tts接口时,一开始可能由于gap切的太大所以前几个text段没有生成成功,查看讯飞控制台也是未调用过接口,这里其实修改gap应该就可以直接出结果了,然后突然想到了多线程处理,网上查阅资料后,修改代码如下:
ArrayList sText = new ArrayList();
ArrayList sAbsolutePath = new ArrayList();
List<Task> sThread = new List<Task>();
//gap切分plaintext,放入sText列表
{
sThread.Add(
Task.Run(() =>
{
string audioName = CreateFileName(".wav");
string audioPath = System.IO.Path.Combine(filePath, audioName);
string resPath = "";
if (speeker != "")
resPath = Text2Audio(t, audioPath, speeker);
else
resPath = Text2Audio(t, audioPath);
sAbsolutePath.Add(audioPath);
})
);
}
Task.WaitAll(sThread.ToArray());
string mergeAudio = mergeAudios(sAbsolutePath, filePath);//合并音频
处理后拼接起来的音频听到的顺序是混乱的,可知这些线程是同步的,怎么定义执行顺序?
后来,查找资料找到了Task.Wait()等待task执行完毕,也就是直接在线程run起来之后等到它执行完毕为止再进行下一个循环,达到了我的要求。
修改foreach{}内代码如下:
foreach (string t in sText)
{
Task<string> task = Task<string>.Run(() =>
{
string audioName = CreateFileName(".wav");
string audioPath = System.IO.Path.Combine(filePath, audioName);
string resPath = "";
if (speeker != "")
resPath = Text2Audio(t, audioPath, speeker);
else
resPath = Text2Audio(t, audioPath);
return resPath;
});
task.Wait();
string res = task.Result;
if (res != "")
sAbsolutePath.Add(res);
}
//Task.WaitAll(sThread.ToArray());