我试图建立一个视频在IOS与文字到语(如TikTok)。 只有这样,才能做到这个,我以为是要合并一视频和音频带AVFoundations,但似乎不可能插入音频文字到语音到.caf文件。
这是什么我想:
public async Task amethod(string[] _text_and_position)
{
string[] text_and_position = (string[])_text_and_position;
double tts_starting_position = Convert.ToDouble(text_and_position[0]);
string text = text_and_position[1];
var synthesizer = new AVSpeechSynthesizer();
var su = new AVSpeechUtterance(text)
{
Rate = 0.5f,
Volume = 1.6f,
PitchMultiplier = 1.4f,
Voice = AVSpeechSynthesisVoice.FromLanguage("en-us")
};
synthesizer.SpeakUtterance(su);
Action<AVAudioBuffer> buffer = new Action<AVAudioBuffer>(asss);
try
{
synthesizer.WriteUtterance(su, buffer);
}
catch (Exception error) { }
}
public async void asss(AVAudioBuffer _buffer)
{
try
{
var pcmBuffer = (AVAudioPcmBuffer)_buffer;
if (pcmBuffer.FrameLength == 0)
{
// done
}
else
{
AVAudioFile output = null;
// append buffer to file
NSError error;
if (output == null)
{
string filePath = Path.Combine(Path.GetTempPath(), "TTS/" + 1 + ".caf");
NSUrl fileUrl = NSUrl.FromFilename(filePath);
output = new AVAudioFile(fileUrl, pcmBuffer.Format.Settings, AVAudioCommonFormat.PCMInt16 , false ,out error);
}
output.WriteFromBuffer(pcmBuffer, out error);
}
}
catch (Exception error)
{
new UIAlertView("Error", error.ToString(), null, "OK", null).Show();
}
}
这是同样的代码,在目标c
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "test 123")
utterance.voice = AVSpeechSynthesisVoice(language: "en")
var output: AVAudioFile?
synthesizer.write(utterance) { (buffer: AVAudioBuffer) in
guard let pcmBuffer = buffer as? AVAudioPCMBuffer else {
fatalError("unknown buffer type: \(buffer)")
}
if pcmBuffer.frameLength == 0 {
// done
} else {
// append buffer to file
if output == nil {
output = AVAudioFile(
forWriting: URL(fileURLWithPath: "test.caf"),
settings: pcmBuffer.format.settings,
commonFormat: .pcmFormatInt16,
interleaved: false)
}
output?.write(from: pcmBuffer)
}
}
这个问题的代码是"合成器。WriteUtterance(苏、缓冲区);"始终崩溃,在阅读其他的职位,我认为这是一个错误,导致将回调的方法(缓冲区)永远不会被称为。
你知道的任何解决办法,这一错误,或任何其他方式达到什么样我想做什么?
感谢您的时间,有一个伟大的日子。
编辑: 我的评论合成器。SpeakUtterance(苏);作为ColeX指出,现在将回调的方法是执行。 不幸的是,我不能存放我的音频文件尚未由于我得到的另一个错误
output = new AVAudioFile(fileUrl, pcmBuffer.Format.Settings, AVAudioCommonFormat.PCMInt16 , false ,out error);
错误:
不能初始化的一个实例类型 'AVFoundation.AVAudioFile':母 'initForWriting:设置:commonFormat:交错:error:'的方法 返回的零。 有可能忽略这一条件的设置 ObjCRuntime.类。ThrowOnInitFailure到错误的。