Playing sound effect in Xcode - ios

I have created a button in Xcode that plays a short sound effect. I am not able to retrigger the button/sound, while it's playing. Therefore I have to wait until the sound has faded to repeat the effect. My intention is that every time I press the button, the sound effect starts to play, even if it is still fading out. Does anybody know how to achieve this? I am using XCode 11.6. My code is below. Many thanks in advance.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "1", ofType: "mp3")!))
audioPlayer.prepareToPlay()
} catch {
print(error)
}
}
#IBAction func Play(_ sender: Any) {
audioPlayer.play()
}
}

You could just initialize the audioPlayer in Play method instead of viewDidLoad.
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func Play(_ sender: Any) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "1", ofType: "mp3")!))
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}

if I understand correctly you want to prevent play again when sound is playing and just play sound when it finished already so
you can make condition according sound is playing or not
(try it in your button action):
if audioPlayer.isPlaying(){
// your statement when sound is playing in other word nothing to do
}else{
// your statement when sound is not playing and you want to start play
audioPlayer.play()
}
https://developer.apple.com/documentation/avfoundation/avaudioplayer/1390139-isplaying

Related

Play random sounds from my projects in Swift 5

I'm new in iOS. I have written this code, which plays only one audio file when tapping a UIButton. I would like to play multiple sound randomly. How to set it? Thank you!
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer: AVAudioPlayer!
#IBAction func playButtonPressed(_ sender: UIButton) {
if let soundURL = Bundle.main.url(forResource: "kompilacja", withExtension: "mp3") {
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
}
catch {
print(error)
}
audioPlayer.play()
}else{
print("Karwasz twarz! Brak pliku audio, Panie!")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Put all of your sound file names in an array and use the randomElement method to choose your sound.
#IBAction func playButtonPressed(_ sender: UIButton) {
let sounds = ["kompilacja", "another sound", "yet another sound"]
guard let sound = sounds.randomElement(),
let soundURL = Bundle.main.url(forResource: sound, withExtension: "mp3") else { return }
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
}
catch {
print(error)
}
audioPlayer.play()
}

Playing sounds with multiple play buttons in iOS

I have to play three types of wav file one at a time. When I click on the Play button in Enter region it plays a sound named "EnterRegion.wav" and clicking on Exit Region play button plays "ExitRegion.wav" audio file.
But for the play button of the Immediate, it plays a sound repeatedly until I press the Stop button.
Both first and second Play buttons are working perfectly for me but when I click on play button in Immediate, it plays sound for the first time but when I click on other play buttons and come back to Immediate play button again, the application throws exceptions. This happens the same when I press stop button and come back to the play button of immediate section. My sample app screenshot and code sample which is in Swift 4 is as below.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var engine: AVAudioEngine!
var enterWavFile: AVAudioFile!
var audioPlayerNode : AVAudioPlayerNode!
var immediaAudioPlayerNode: AVAudioPlayerNode!
var exitWavFile: AVAudioFile!
var flatlineWavFile:AVAudioFile!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
engine = AVAudioEngine()
enterWavFile = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "EnterRegion", ofType: "wav")!))
exitWavFile = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "ExitRegion", ofType: "wav")!))
flatlineWavFile = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "Immediate", ofType: "wav")!))
}
#IBAction func playEnterRegionSound(_ sender: UIButton) {
playSound(audioFile: enterWavFile)
}
#IBAction func playExitRegion(_ sender: UIButton) {
playSound(audioFile: exitWavFile)
}
func playSound(audioFile: AVAudioFile) {
audioPlayerNode = AVAudioPlayerNode()
if(audioPlayerNode.isPlaying){
audioPlayerNode.stop()
}
if(engine.isRunning){
engine.stop()
engine.reset()
}
engine.attach(audioPlayerNode)
engine.connect(audioPlayerNode, to: engine.mainMixerNode, format: audioFile.processingFormat)
audioPlayerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)
// Start the audio engine
engine.prepare()
try! engine.start()
audioPlayerNode.play()
}
#IBAction func playImmediateFlatline(_ sender: UIButton) {
immediaAudioPlayerNode = AVAudioPlayerNode()
if(immediaAudioPlayerNode.isPlaying){
print("immediateAudioPlayerNode Playing")
immediaAudioPlayerNode.stop()
}
if(engine.isRunning){
print("engine Running")
engine.stop()
engine.reset()
}
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: flatlineWavFile.processingFormat, frameCapacity: AVAudioFrameCount( flatlineWavFile.length))!
try? flatlineWavFile.read(into: audioFileBuffer)
engine.attach(immediaAudioPlayerNode)
engine.connect(immediaAudioPlayerNode, to: engine.mainMixerNode, format: flatlineWavFile.processingFormat)
immediaAudioPlayerNode.scheduleBuffer(audioFileBuffer, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)
// Start the audio engine
engine.prepare()
try! engine.start()
immediaAudioPlayerNode.play()
}
#IBAction func stopImmediateFlatline(_ sender: UIButton) {
if (immediaAudioPlayerNode.isPlaying){
print("immediateAudioPlayerNode playing")
immediaAudioPlayerNode.pause()
}
if(engine.isRunning){
print("engine Running")
engine.stop()
engine.reset()
}
}
}
I am unable to locate why this is happening and could you please help me to fix the issue or any suggestions.
code for playing the sound
var player1: AVAudioPlayer?
var player2: AVAudioPlayer?
var player3: AVAudioPlayer?
func playSound() {
guard let url = Bundle.main.url(forResource: "Music", withExtension: "wav") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
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 }
// read this line of code for you to play the wav file you wanted to play ang then trigger it using a button.
player.play()
} catch let error {
print(error.localizedDescription)
}
}

Using a UITapGestureRecognizer to play/pause audio

This seems like a fairly simple task, but have been trying for a while to figure it out... I have set up a UITapGestureRecognizer to play an audio file from the bundle. That works well, but I would like to have the user be able to tap the item again and have the audio stop. Basically have the ability to start and replay the audio by tapping the object with single taps.
#IBAction func page1Tapped(_ sender: UITapGestureRecognizer) {
let url:URL = Bundle.main.url(forResource: "Opening Solo", withExtension: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: nil)
} catch {
return
}
audioPlayer.prepareToPlay()
audioPlayer.play()
if audioPlayer.isPlaying {
audioPlayer.stop()
} else {
audioPlayer.play()
}
}
Here is the function that I have created. The audio only plays and just restarts at 0 with every tap at this point.
Any help is incredibly appreciated!!
This because every click creates a new re-assings the old instance , you can declare the player as instance variable
var audioPlayer:AVAudioPlayer?
and check it's value before assigning
if audioPlayer == nil
{
audioPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: nil)
audioPlayer.play()
}
else
{
if audioPlayer.isPlaying {
audioPlayer.stop()
} else {
audioPlayer.play()
}
}

Swift 3 - AVAudioPlayer is not playing sound

I have a simple program which has one button and its only function is to trigger an sound output.
The app works perfectly when tested on an iOS simulator, but when I test it on an iPhone, the app is not playing any sound. I have tested this on two iPhones.
This was working once upon a time. I don't know if updating to iOS 10 has caused the issue.
Here is my code:
//Code
import UIKit
import AVFoundation
var audioPlayer = AVAudioPlayer()
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let music = Bundle.main.path(forResource: "sound", ofType: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music! ))
}
catch{
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playSound(_ sender: UIButton) {
audioPlayer.play()
}
}
Would be grateful for any answers.
Thanks.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let music = Bundle.main.path(forResource: "sound", ofType: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music! ))
audioPlayer.delegate = self
audioPlayer.preparedToPlay()
}
catch{
print(error)
}
}
import UIKit
import AVFoundation
class ViewController: UIViewController {
var btnSound: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
//locate resource
let path = Bundle.main.path(forResource: "sound", ofType: "wav")
let music = URL(fileURLWithPath: path!)
do{
try btnSound = AVAudioPlayer(contentsOf: music)
btnSound.prepareToPlay()
} catch let err as NSError{
print(err.debugDescription)
}
}
}
func playSound(){
if btnSound.isPlaying{
//stop playing sound file if already playing
btnSound.stop()
}
//play sound
btnSound.play()
}
#IBAction func playSound(sender: UIButton){
//call playSound function on pressing the button you attach this function to
playSound()
}

Audio not playing in simulator

I have three buttons and I'm trying to get a sound to play with each button.
The sounds don't play on the simulator wondering what was going wrong in my code.
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func mmm_sound(sender: UIButton) {
playSound("mmm")
}
#IBAction func aww_sound(sender: UIButton) {
playSound("aww")
}
#IBAction func maniacal_sound(sender: UIButton) {
playSound("hah")
}
//sound function
func playSound(soundName: String)
{
let sound = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource(soundName, ofType: "wav")!)
do{
let audioPlayer = try AVAudioPlayer(contentsOfURL:sound)
audioPlayer.prepareToPlay()
audioPlayer.play()
}catch {
print("Error getting the audio file")
}
}
}
You should make AVAudioPlayer as global variable as local variable of 'AVAudioPlayer' get deallocate before it plays, your code can like this
//at top
var audioPlayer = AVAudioPlayer()
func playSound(soundName: String)
{
let sound = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource(soundName, ofType:"wav")!)
do{
audioPlayer = try AVAudioPlayer(contentsOfURL:sound)
audioPlayer.prepareToPlay()
audioPlayer.play()
}catch {
print("Error getting the audio file")
}
}

Resources