利用 iOS SDK 各式型別生成東西,設定它的屬性和呼叫方法

1 用 AVSpeechSynthesizer 講話

讓不敢開口告白的我也可以透過程式講話啦~~~

import AVFoundation
let speechUtterance = AVSpeechUtterance(string:"There goes my heart beating. Cause you are the reason. I'm losing my sleep. Please come back now.")
speechUtterance.voice = AVSpeechSynthesisVoice(language: "English")
speechUtterance.pitchMultiplier = 1.0
speechUtterance.rate = 0.3
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(speechUtterance)

2 用 AVPlayer 播音樂

參考同學的作業,搜尋了一首我心目中的神曲

從itunes搜尋歌曲,右鍵檢視原始碼後,找到.m4a那串網址複製貼上後產出如下:

import AVFoundation
let url = URL(string: "https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview122/v4/8b/38/ef/8b38ef16-89db-e71d-9204-8f8298439747/mzaf_3048223299158318777.plus.aac.ep.m4a")
let player = AVPlayer(url: url!)
player.play()