IOS
iOS Swift AVPlayer를 이용해서 로컬 사운드를 재생해보자.
일용직 코딩노동자
2021. 10. 1. 10:30
728x90
반응형
import AVKit
import AVFoundation
2개 먼저 임포트 해주세요.
전역변수로
var avPlay: AVAudioPlayer?
하나 선언해줍니다.
그리고 함수를 하나 만들어줍니다.
private func playSound(name: String, type: String) { //사운드플레이어 ( 파일명, 확장자 )
let url = Bundle.main.url(forResource: name, withExtension: type)
if let url = url{
do{
avPlay = try AVAudioPlayer(contentsOf: url)
guard let sound = avPlay
else{
return
}
sound.prepareToPlay()
sound.play()
}
catch let error{
print(error.localizedDescription)
}
}
}
self.playSound(name: "fileName", type: "m4a")
이렇게 사용해주시면 플레이가 끝납니다.
728x90
반응형