play sound swift stack overflow
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:
- declare empty string in swift
- xcode label rotate text
- swift session.input
- xcode disable a button
- conert data to string swift
- swift for loop index
- format decimal place swift
- custom binding swiftui
- xcode macosx have view resize when window size changed
- how to screen record swift stackoverflow
- textField.characther range
- swift play video from url
- sprite kitYourNextScene
- uitextfield placeholder text
- xcode enable a button after a text field is filled
- uiimageview add tap gesture swift
- remove cocoapods swiftr
- how to use alamofire pod in swift 5
- use timer swift
- swift stirng to date
- how to delete from list tvos swiftui coredata
- array of button listeners swift
- xcode create image from calayer
- shuffle array swift