playing a sound in swift
import AVFoundation
var player: AVAudioPlayer?
func playSound() {
guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
/* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
/* iOS 10 and earlier require the following line:
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3) */
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}
Also in Swift:
- how to insert element at start of the array ios swift
- how to dismiss keyboard swiftui
- chating App in swift
- index string swift
- swift access appdelegate from viewcontroller
- swift empty an array
- playing a sound in swift
- swift generate random number
- markdown
- prevent iphone to sleep swift
- fetch request core data
- context.fetch() in swift
- how to fetch data from server in swift
- get address from latitude and longitude in swift
- swift double v float
- how to unwrap arrays with optional value in swift
- how do i have countdown timer in swift stackoverflow
- add shadow to collection view cell swift
- use timer swift
- rounded ios button
- UI API called on a background thread
- swift has Top Notch
- add top corner radius swift
- how to show notification icon on tabbar item swift