The easiest way to play audio in Swift
play(fileNamed:fromCache:completion:)play(filesNamed:fromCache:completion:)play(url:fromCache:completion:)play(urls:fromCache:completion:)play(data:fromCache:completion:)
Audio.play(systemSoundID:as:completion:)Audio.vibrate(completion:)Audio.flashScreen(completion:)Audio.impact(style:intensity:)
Audio.replaceAudioTags(inHTML:with:)Audio.removeAudioTags(inHTML:)Audio.extractUrlsFromAudioTags(inHTML:)Audio.hasValidAudioUrlsFromAudioTags(inHTML:)playAll(inHTML:fromCache:completion:)playFirst(inHTML:fromCache:completion:)playLast(inHTML:fromCache:completion:)
Audio.shared.play(url: "https://www.soundjay.com/button/beep-01a.wav") { error in if let error = error { print("An error occurred: \(error)") } else { print("Done!") } }
// Plays in sequence Audio.shared.play(urls: [ "https://www.soundjay.com/button/beep-01a.wav", // Plays this first "https://www.soundjay.com/button/beep-01a.wav", // Plays this second "https://www.soundjay.com/button/beep-01a.wav" // Plays this third ]) { finished, error in if finished { print("All done!") } else if let error = error { print("An error occurred: \(error)") } else { print("On to the next audio URL!") } }
// Create a new reusable Audio environment. // If you try to play audio at the same time in the same environment, // the first one stops and the second one starts. // In different environments, they play over each other. let myAudio = Audio() myAudio.play(fileNamed: "beep.wav") print(myAudio.isPlaying) // true myAudio.pause() print(myAudio.isPlaying) // false myAudio.resume()
let html = """ <!DOCTYPE html> <html> <body> <audio controls src="https://example.com/audio"></audio> <audio controls src="https://example.com/other-audio"></audio> </body> </html> """ Audio.shared.playAll(inHTML: html) Audio.shared.playFirst(inHTML: html) Audio.shared.playLast(inHTML: html)
// Play a key pressed sound Audio.play(systemSoundID: 1103) { print("Finished playing sound") } // Vibrate the phone Audio.vibrate() // Flash white on the screen Audio.flashScreen { print("That was bright!") } // Performs an short impact on the iOS device that the user can feel Audio.impact() Audio.impact(style: .rigid) Audio.impact(style: .heavy, intensity: 0.5)