V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
garyox64
V2EX  ›  Swift

[ Swift ] synthesizer.speak 怎么出声?

  •  
  •   garyox64 · Sep 29, 2022 · 1387 views
    This topic created in 1307 days ago, the information mentioned may be changed or developed.
    #!/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 replies    2022-10-06 13:43:49 +08:00
    butanediol2d
        1
    butanediol2d  
       Oct 6, 2022
    你在最后加一个 sleep 试一下,可能是还没等开口说话程序就退出了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5827 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 03:21 · PVG 11:21 · LAX 20:21 · JFK 23:21
    ♥ Do have faith in what you're doing.