04 利用iOS SDK 各式型別生成東西,設定它的屬性和呼叫方法🕹
Published in
7 min readDec 12, 2022
用 AVPlayer 播音樂
import AVFoundation
let url = URL(string: "https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview122/v4/6e/51/04/6e510466-e666-e2f4-3f61-bccb9d9c8b51/mzaf_11466994686483297798.plus.aac.p.m4a")
let player = AVPlayer(url: url!)
player.play()
播放歌曲:六哲- 畢竟深愛過
播放時會擷取第二段主歌,用程式讓音樂播放出來有種莫名的成就感誒🤣
用 AVPlayerViewController 播影片
import AVKit
import PlaygroundSupport
let url = URL(string: "https://movietrailers.apple.com/movies/dreamworks/puss-in-boots-the-last-wish/puss-In-boots-the-last-wish-trailer-1b_h1080p.mov")
let player = AVPlayer(url: url!)
let controller = AVPlayerViewController()
controller.player = player
PlaygroundPage.current.liveView = controller
player.play()
現正播映:鞋貓劍客2 Puss In Boots: The Last Wish
可從網頁裡找到影片連結,從右鍵選單點選 Copy Link
利用 SFSafariViewController 顯示網頁
import SafariServices
import PlaygroundSupport
let url = URL(string: "https://medium.com/@choumina838")
let controller = SFSafariViewController(url: url!)
PlaygroundPage.current.liveView = controller
顯示地圖 & 地圖標記的方法
import MapKit
import PlaygroundSupport
let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
mapView.region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(25.013847, 121.467027), latitudinalMeters: 500, longitudinalMeters: 500)
PlaygroundPage.current.liveView = mapView
mapView.mapType = .hybrid
//加入座標
let 板橋大遠百 = MKPointAnnotation()
板橋大遠百.title = "大遠百"
板橋大遠百.coordinate = CLLocationCoordinate2D(latitude: 25.013847, longitude: 121.467027)
板橋大遠百.subtitle = "很大的遠百"
mapView.addAnnotation(板橋大遠百)
列印時間
import Foundation
//列印時間
var time = Date()
print(time)
//增加“秒”數,計算3小時50分20秒等於13820秒
time.addTimeInterval(13820)
print(time)
//時間變成特定格式字串
import Foundation
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd/"
let dateString = dateFormatter.string(from: now)
//取得今天幾月幾號的數字
import Foundation
let today = Date()
let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: today)
let month = dateComponents.month
let day = dateComponents.day
使用 AVSpeechSynthesizer 講話() 喵電感應😵💫
import AVFAudio
let utterance = AVSpeechUtterance(string: "喵喵喵喵喵喵喵電感應,細胞在細胞在共鳴 內心在呼應")
//語言代碼:https://gist.github.com/vitoziv/9108407
utterance.voice = AVSpeechSynthesisVoice(language: "zh-TW")
//rate 0~1 數字越大越快
utterance.rate = 0.5
//volume 0~1
utterance.volume = 0.6
//pitch 0.5~2.0 數字越大pitch越高
utterance.pitchMultiplier = 0.6
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)
關鍵技術:
- 地圖: MapKit,MKMapView,MKCoordinateRegion
- 播音樂: AVFoundation,URL,AVPlayer
- 播影片: AVKit,AVPlayerViewController
- 網頁: SafariServices,SFSafariViewController
- 時間: Foundation,Date,DateFormatter,Calendar,DateComponents,Void
- App 講話: AVFAudio,AVSpeechSynthesizer,AVSpeechUtterance,AVSpeechSynthesisVoice
- playground: PlaygroundSupport,PlaygroundPage,liveView,type property