Created
September 3, 2020 19:39
-
-
Save carlynorama/d737e9cd8a2d4faeaf71b96aca56f79d to your computer and use it in GitHub Desktop.
Playing sounds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AVFoundation | |
// ---------- Custom Sounds ---------- | |
//https://www.hackingwithswift.com/example-code/media/how-to-play-sounds-using-avaudioplayer | |
var bombSoundEffect: AVAudioPlayer? | |
let path = Bundle.main.path(forResource: "example.mp3", ofType:nil)! | |
let url = URL(fileURLWithPath: path) | |
do { | |
bombSoundEffect = try AVAudioPlayer(contentsOf: url) | |
bombSoundEffect?.play() | |
} catch { | |
// couldn't load file :( | |
} | |
//If you want to stop the sound, you should use its stop() method. | |
// ---------- System Sounds ---------- | |
//https://stackoverflow.com/a/31126202 | |
//https://github.com/TUNER88/iOSSystemSoundsLibrary | |
//https://developer.apple.com/documentation/audiotoolbox/audio_services#//apple_ref/c/func/AudioServicesPlaySystemSound | |
// create a sound ID, in this case its the tweet sound. | |
let systemSoundID: SystemSoundID = 1016 | |
// to play sound | |
AudioServicesPlaySystemSound (systemSoundID) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment