如何储存的文字到语音频音频文件(IOS)

0

的问题

我试图建立一个视频在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到错误的。

avfoundation c# ios objective-c
2021-11-22 18:44:34
1

最好的答案

1

错误只是显示了 An AVSpeechUtterance shall not be enqueued twice .

因此,停止使它说话和写在同一时间。

我用你的代码和注释出来 synthesizer.SpeakUtterance(su); 错了。

更新

根据我的测试,它不允许创造额外的文件夹,所以除去 TTS/ 部分,只保留文件的名单。

string filePath = Path.Combine(Path.GetTempPath(),  1 + ".caf");
2021-11-26 04:28:54

谢谢,ColeX,你的回答是有帮助的解决问题所提到的,遗憾的是,另一个问题合并起来,我的编辑我原来的岗位,你能看看它吗? 谢谢你这么多你的时间。 (:
Jaime Santos

检查我的更新。
ColeX - MSFT

谢谢你的帮助ColeX,我已经更新我的代码,以及现在的"。caf"的文件是正在成功地产生的,但是,本文件不包含音频规定,这是一个0秒长的音频文件。 任何想法,这可能是为什么发生?
Jaime Santos

好吧,我只是发现我的缺失,"AVAudioFile输出=null;"应外面的方法,否则,它是将自身重置。 谢谢你的帮助ColeX,我要去标记你的消息,作为回答,有一个愉快的一天。 (:
Jaime Santos

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................