Snažím se vytvořit video v IOS s Text-to-speech (jako TikTok dělá). Jediný způsob, jak to udělat, že jsem si myslel, že sloučit video a audio s AVFoundations, ale zdá se to nemožné chcete-li vložit do audio text-to-speech, do .soubor s příponou caf.
To je to, co jsem se snažil:
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();
}
}
Je to stejný kód v objective-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)
}
}
Problémem tohoto kódu je, že "syntezátor.WriteUtterance(su, buffer);" vždy pády, po přečtení dalších příspěvků jsem přesvědčen, že je to chyba, že výsledky v callback metody (buffer) nikdy volána.
Znáte nějaké řešení, jak tuto chybu nebo jakýkoli jiný způsob, jak dosáhnout toho, co jsem se snaží udělat?
Díky za váš čas, přeji hezký den.
EDIT: Poznamenal jsem syntezátor.SpeakUtterance(su); jako ColeX poukázal na to, a nyní metodu zpětného volání je spuštěn. Bohužel, nemůžu ukládat své audio do souboru, ale od doby, co jsem dostat další chybu v
output = new AVAudioFile(fileUrl, pcmBuffer.Format.Settings, AVAudioCommonFormat.PCMInt16 , false ,out error);
CHYBA:
Nelze inicializovat instanci typu 'AVFoundation.AVAudioFile': nativní 'initForWriting:nastavení:commonFormat:interleaved:error:' metoda vrátil nulová. Je možné ignorovat tento stav nastavením ObjCRuntime.Třídy.ThrowOnInitFailure na hodnotu false.