音を出す(AVFoundationを使用して) Swift3.0
AVFoundationを使用してあるボタンを押すと音を出すプログラム(Swift3.0)
以下プログラム記述
import UIKit
// AVFoundationフレームワークをインポートする
import AVFoundation
class ViewController:UIViewController{
//音声を制御するための変数player
var player:AVAudioPlayer?
@IBAction func play(_ sender: AnyObject) {
//サウンドファイルを読み込む
let url =
Bundle.main.bundleURL.appendingPathCompone("●●●.mp3")
do{
try player = AVAudioPlayer(contentsOf: url)
}catch{
print("Error!")
}
//音を再生する
player?.play()
}
以上
★まとめ★
・do-try-catch の構文の理解ができていない
・AVFoundationを使用するためには、url 、Bundle、 パス等の意味がわからないとダメかも。
・まずは、丸暗記で使っていこう。