V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
garyox64
V2EX  ›  Swift

[ Swift ] synthesizer.speak 怎么出声?

  •  
  •   garyox64 · 2022-09-29 18:29:54 +08:00 · 706 次点击
    这是一个创建于 546 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/usr/bin/env swift
    import AVFoundation
    // Create an utterance.
    let utterance = AVSpeechUtterance(string: "The quick brown fox jumped over the lazy dog.")
    
    // Configure the utterance.
    utterance.rate = 0.57
    utterance.pitchMultiplier = 0.8
    utterance.postUtteranceDelay = 0.2
    utterance.volume = 0.8
    
    // Retrieve the British English voice.
    let voice = AVSpeechSynthesisVoice(language: "en-GB")
    
    // Assign the voice to the utterance.
    utterance.voice = voice
    // Create a speech synthesizer.
    let synthesizer = AVSpeechSynthesizer()
    
    // Tell the synthesizer to speak the utterance.
    synthesizer.speak(utterance)
    synthesizer.speak(AVSpeechUtterance(string: "Hello The quick brown fox jumped over the lazy dog."))
    
    var myString = "Hello, World!"
     
    print(myString)
    

    为啥这个没有声音,synthesizer 不能用 swsh 脚本使用? 我是纯小白,也没学过 swift 今天发现 mac 的朗读功能挺有意思的就想调一下这个接口玩玩,但是发现没动静 感觉我的语法没问题啊,应该是 使用姿势不对吧,求指点要怎么用

    目前就是想这个 出声音,求解

    1 条回复    2022-10-06 13:43:49 +08:00
    butanediol2d
        1
    butanediol2d  
       2022-10-06 13:43:49 +08:00
    你在最后加一个 sleep 试一下,可能是还没等开口说话程序就退出了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3330 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 11:23 · PVG 19:23 · LAX 04:23 · JFK 07:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.