iOS Task#18 | iOS SDK — AVPlayer
Published in
Dec 11, 2021
iOS SDK (Software Development Kit) —
Apple提供給 iOS 開發者的軟體開發工具。開發者可以在Apple提供的開發環境 XCode 中,使用各種工具來輔助開發。
幾個關鍵的技術包含
- Music : AVFoundation, URL, AVPlayer
- Video : AVKit, AVPlayerViewController
- Website : SafariServices, SFSafariViewController
- Time/Date : Foundation, Date, DateFormatter, Calendar, DateComponents, Void
- Speech : AVFoundation, AVSpeechSynthesizer, AVSpeechUtterance, AVSpeechSynthesisVoice
- Playground : PlaygroundSupport, PlaygroundPage, liveView, type property
- Map : MapKit, MKMapView, MKCoordinateRegion
Music — AVPlayer
Step 1. 查詢iTunes音樂、電影的preview連結
等等會要嵌入音樂的URL路徑或網址,但因為Youtube被沒有開放下載他的音樂,所以使用Youtube網址短時間就會失效。我們可以從Apple Music音樂庫來找Preview連結。
Step 2. 在搜尋框內輸入要找的音樂,並找到自己要的曲子
Step 3. 複製網址裡面的歌曲ID:
Step 4. 用底下的網址來查詢preview連結
http://itunes.apple.com/tw/lookup?id=剛剛查到的歌曲ID
Step 5. 這時候會下載歌曲資訊的文字檔
Step 6. ⌘+F 搜尋”previewurl”
Step 7. 找到試聽連結
Step 8. 貼到Xcode完成程式碼
import AVFoundationlet url = URL(string: "http://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview125/v4/9d/f1/95/9df1957a-5b85-b365-aceb-f71aa146252/mzaf_1089189607902396480.plus.aac.p.m4a"let player = AVPlayer(url: url!)player.play()