【问题标题】:Save text to audio file programmatically以编程方式将文本保存到音频文件
【发布时间】:2015-04-17 14:25:16
【问题描述】:

可以将文本保存为音频文件,如屏幕截图所示。现在我的问题是如何以编程方式做同样的事情。

非常感谢任何帮助。

【问题讨论】:

    标签: objective-c macos


    【解决方案1】:

    NSSpeechSynthesizer 完成这项工作。

    假设我们有财产:

    @property  NSSpeechSynthesizer *synth;
    

    我们这样初始化:

    synth = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
    

    我们可以做到:

    NSURL *saveURL = [NSURL fileURLWithPath:[@"~/Desktop/SpokenText.aiff" stringByExpandingTildeInPath]];
    [synth startSpeakingString:@"A spoken short text" toURL:saveURL];
    

    在保存之前,我们可能想改变声音或音量:

    [synth setVolume:0.5f];
    [synth setVoice:voiceID];
    

    value for volume 以浮点单位表示,范围从 0.0 到 1.0,voiceIDvoice identifier




    要查看所有可用的语音标识符:

    NSArray *voiceIdentifiers = [NSSpeechSynthesizer availableVoices];
    NSLog(@"%li %@",voiceIdentifiers.count,voiceIdentifiers);
    

    OS X 10.10.3 上输出并安装所有声音:

    101 (
        "com.apple.speech.synthesis.voice.Agnes",
        "com.apple.speech.synthesis.voice.Albert",
        "com.apple.speech.synthesis.voice.Alex",
        "com.apple.speech.synthesis.voice.alice.premium",
        "com.apple.speech.synthesis.voice.allison.premium",
        "com.apple.speech.synthesis.voice.alva.premium",
        "com.apple.speech.synthesis.voice.amelie.premium",
        "com.apple.speech.synthesis.voice.angelica.premium",
        "com.apple.speech.synthesis.voice.anna.premium",
        "com.apple.speech.synthesis.voice.audrey.premium",
        "com.apple.speech.synthesis.voice.aurelie.premium",
        "com.apple.speech.synthesis.voice.ava.premium",
        "com.apple.speech.synthesis.voice.BadNews",
        "com.apple.speech.synthesis.voice.Bahh",
        "com.apple.speech.synthesis.voice.Bells",
        "com.apple.speech.synthesis.voice.Boing",
        "com.apple.speech.synthesis.voice.Bruce",
        "com.apple.speech.synthesis.voice.Bubbles",
        "com.apple.speech.synthesis.voice.carlos.premium",
        "com.apple.speech.synthesis.voice.carmit.premium",
        "com.apple.speech.synthesis.voice.catarina.premium",
        "com.apple.speech.synthesis.voice.Cellos",
        "com.apple.speech.synthesis.voice.cem.premium",
        "com.apple.speech.synthesis.voice.chantal.premium",
        "com.apple.speech.synthesis.voice.claire.premium",
        "com.apple.speech.synthesis.voice.damayanti.premium",
        "com.apple.speech.synthesis.voice.daniel.premium",
        "com.apple.speech.synthesis.voice.Deranged",
        "com.apple.speech.synthesis.voice.diego.premium",
        "com.apple.speech.synthesis.voice.ellen.premium",
        "com.apple.speech.synthesis.voice.ewa.premium",
        "com.apple.speech.synthesis.voice.federica.premium",
        "com.apple.speech.synthesis.voice.felipe.premium",
        "com.apple.speech.synthesis.voice.fiona.premium",
        "com.apple.speech.synthesis.voice.Fred",
        "com.apple.speech.synthesis.voice.GoodNews",
        "com.apple.speech.synthesis.voice.henrik.premium",
        "com.apple.speech.synthesis.voice.Hysterical",
        "com.apple.speech.synthesis.voice.ioana.premium",
        "com.apple.speech.synthesis.voice.iveta.premium",
        "com.apple.speech.synthesis.voice.joana.premium",
        "com.apple.speech.synthesis.voice.jorge.premium",
        "com.apple.speech.synthesis.voice.juan.premium",
        "com.apple.speech.synthesis.voice.Junior",
        "com.apple.speech.synthesis.voice.kanya.premium",
        "com.apple.speech.synthesis.voice.karen.premium",
        "com.apple.speech.synthesis.voice.kate.premium",
        "com.apple.speech.synthesis.voice.Kathy",
        "com.apple.speech.synthesis.voice.katya.premium",
        "com.apple.speech.synthesis.voice.klara.premium",
        "com.apple.speech.synthesis.voice.kyoko.premium",
        "com.apple.speech.synthesis.voice.laura.premium",
        "com.apple.speech.synthesis.voice.lee.premium",
        "com.apple.speech.synthesis.voice.lekha.premium",
        "com.apple.speech.synthesis.voice.luca.premium",
        "com.apple.speech.synthesis.voice.luciana.premium",
        "com.apple.speech.synthesis.voice.magnus.premium",
        "com.apple.speech.synthesis.voice.mariska.premium",
        "com.apple.speech.synthesis.voice.markus.premium",
        "com.apple.speech.synthesis.voice.mei-jia.premium",
        "com.apple.speech.synthesis.voice.melina.premium",
        "com.apple.speech.synthesis.voice.milena.premium",
        "com.apple.speech.synthesis.voice.moira.premium",
        "com.apple.speech.synthesis.voice.monica.premium",
        "com.apple.speech.synthesis.voice.nicolas.premium",
        "com.apple.speech.synthesis.voice.nikos.premium",
        "com.apple.speech.synthesis.voice.nora.premium",
        "com.apple.speech.synthesis.voice.oliver.premium",
        "com.apple.speech.synthesis.voice.oskar.premium",
        "com.apple.speech.synthesis.voice.otoya.premium",
        "com.apple.speech.synthesis.voice.paola.premium",
        "com.apple.speech.synthesis.voice.paulina.premium",
        "com.apple.speech.synthesis.voice.petra.premium",
        "com.apple.speech.synthesis.voice.Organ",
        "com.apple.speech.synthesis.voice.Princess",
        "com.apple.speech.synthesis.voice.Ralph",
        "com.apple.speech.synthesis.voice.samantha.premium",
        "com.apple.speech.synthesis.voice.sara.premium",
        "com.apple.speech.synthesis.voice.satu.premium",
        "com.apple.speech.synthesis.voice.serena.premium",
        "com.apple.speech.synthesis.voice.sin-ji.premium",
        "com.apple.speech.synthesis.voice.soledad.premium",
        "com.apple.speech.synthesis.voice.susan.premium",
        "com.apple.speech.synthesis.voice.tarik.premium",
        "com.apple.speech.synthesis.voice.tessa.premium",
        "com.apple.speech.synthesis.voice.thomas.premium",
        "com.apple.speech.synthesis.voice.ting-ting.premium",
        "com.apple.speech.synthesis.voice.tom.premium",
        "com.apple.speech.synthesis.voice.Trinoids",
        "com.apple.speech.synthesis.voice.veena.premium",
        "com.apple.speech.synthesis.voice.Vicki",
        "com.apple.speech.synthesis.voice.Victoria",
        "com.apple.speech.synthesis.voice.Whisper",
        "com.apple.speech.synthesis.voice.xander.premium",
        "com.apple.speech.synthesis.voice.yannick.premium",
        "com.apple.speech.synthesis.voice.yelda.premium",
        "com.apple.speech.synthesis.voice.yuna.premium",
        "com.apple.speech.synthesis.voice.yuri.premium",
        "com.apple.speech.synthesis.voice.Zarvox",
        "com.apple.speech.synthesis.voice.zosia.premium",
        "com.apple.speech.synthesis.voice.zuzana.premium"
    )
    

    【讨论】:

    • 试过了 bool b = [synthesizer startSpeakingString:@"我已经开始说话了" toURL:[NSURL URLWithString:@"/Desktop"]];返回 true 但找不到文件
    • 酷,谢谢。我还在代码中添加了 URL 创建,因此答案更清楚。
    • 太棒了!是否有针对同一案例的 iOS 解决方案?
    【解决方案2】:

    从 iOS 7 开始,Apple 提供以下 API...

    https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVSpeechSynthesizer_Ref/Reference/Reference.html

    看到这个答案https://stackoverflow.com/a/17465494/215748

    #import <AVFoundation/AVFoundation.h>
    …
    AVSpeechUtterance *utterance = [AVSpeechUtterance 
                                speechUtteranceWithString:@"Hello world"];
    AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
    [synth speakUtterance:utterance];
    

    【讨论】:

    • 我的问题是保存为音频文件,而不是通过扬声器说话。相反,我需要 mac 支持。
    • 录制音频不是个好主意
    • 你怎么交流是我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多