Playing a very short sound (~0.5s) produces a hiccup (like a lag) in my SpriteKit iOS game programmed in Swift. In other questions, I read that I should prepareToPlay() the sound, which I did.
I even used a variable (soundReady) to check if the sound is prepared before playing it. I also re-prepare the sound whenever it is finished playing (audioPlayerDidFinishPlaying()). Here are the relevant parts of the code:
class GameScene: SKScene, AVAudioPlayerDelegate {
var splashSound = NSURL()
var audioPlayer = AVAudioPlayer()
var soundReady = false
override func didMoveToView(view: SKView) {
let path = NSBundle.mainBundle().pathForResource("plopSound", ofType: "m4a")
splashSound = NSURL(fileURLWithPath: path)
audioPlayer = AVAudioPlayer(contentsOfURL: splashSound, error: nil)
audioPlayer.delegate = self
soundReady = audioPlayer.prepareToPlay()
}
func playSound(){
if(soundReady){
audioPlayer.play()
soundReady = false
}
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool){
//Prepare to play after Sound finished playing
soundReady = audioPlayer.prepareToPlay()
}
}
I have no idea where I've gone wrong on this one. I feel like I have tried everything (including, but not limited to: only preparing once, preparing right after playing, not using a variable, but just prepareToPlay()).
Additional information:
The sound plays without delay.
How quickly the sound is played after the last finish does not seem to impact the lag.
I ran into this same problem and played the sound in the backgroundQueue.
This is a good example: https://stackoverflow.com/a/25070476/586204.
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
audioPlayer.play()
})
Just adding a Swift 3 version of the solution from #brilliantairic.
DispatchQueue.global().async {
audioPlayer.play()
}
When I called play multiple times it would cause bad access. I believe the player is being deallocated since this is not thread safe. I created a serial queue to alleviate this problem.
class SoundManager {
static let shared = SoundManager()
private init() {
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try? AVAudioSession.sharedInstance().setActive(true)
}
private let serialQueue = DispatchQueue(label: "SoundQueue", qos: .userInitiated)
private var player: AVAudioPlayer?
static func play(_ sound: Sound) {
shared.play(sound)
}
func play(_ sound: Sound) {
guard let url = Bundle.main.url(forResource: sound.fileName, withExtension: "mp3")
else { return }
do {
try serialQueue.sync {
self.player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3)
DispatchQueue.main.async {
self.player?.play()
}
}
} catch let error as NSError {
print("error: \(error.localizedDescription)")
}
}
}
Related
Want to play multiple audio files in the same ViewController. When a user clicks on Button1 the file1.mp3 should be loaded into ViewController and when user hit play same should play and for button2 file2.mp3 should load and play.
I know how to do this for a single file but for multiple files I have to create ViewController again and again which is so hard. This is my code for single file
#IBAction func buttonPressed(_ sender: UIButton) {
if player.isPlaying {
player.stop()
sender.setImage(UIImage(named:"play.png"),for: .normal)
} else {
player.delegate = self
player.play()
player.numberOfLoops = 107
sender.setImage(UIImage(named:"pause.png"),for: .normal)
}
}
do {
let audioPlayer = Bundle.main.path(forResource: "chalisa", ofType: "mp3")
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer!) as URL)
}catch {
//ERROR
}
You can use Tableview List all songs Name and Play Button use to Multiple audio files use in one viewcontroller
import AVFoundation
//MARK:- Variable Declaration
var player: AVAudioPlayer?
func playMusic() {
guard let url = Bundle.main.url(forResource: "chalisa", withExtension: "mp3") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
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)
}
}
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)
}
}
I have background mode active for audio and I use this method to play files, but I can't control them when the screen is locked.
func loadSound(resource: String, soundExtension: String) {
guard let url = Bundle.main.url(forResource: resource, withExtension: soundExtension) else {
return
}
UIApplication.shared.beginReceivingRemoteControlEvents()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
} catch let error {
print(error.localizedDescription)
}
}
For someone with the same problem, call this method in the question method
fileprivate let remoteCommandCenter = MPRemoteCommandCenter.shared()
func activateControlCenterCommands() {
remoteCommandCenter.playCommand.addTarget(self, action: #selector(handlePlayCommandEvent(_:)))
remoteCommandCenter.pauseCommand.addTarget(self, action: #selector(handlePauseCommandEvent(_:)))
remoteCommandCenter.playCommand.isEnabled = true
remoteCommandCenter.pauseCommand.isEnabled = true
}
For simplicity, assume this View says alphabet letters in button when you click on said button.
I have been attempting to play the audio files associated with each button, but the files never play. The buttons, individually, are never given outlets, but they are all connected to the function on Touch Up Inside.
class UIAlphabetController: UIViewController, AVAudioPlayerDelegate{
#IBAction func PlaySound(Sender: UIButton) {
let soundname:String = Sender.currentTitle!
var player: AVAudioPlayer?
let url = Bundle.main.url(forResource: soundname, withExtension: "mp3")
do {
player = try AVAudioPlayer(contentsOf: url!)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error { print(error.localizedDescription) }
}
}
Since I have had this problem, the AQDefaultDevice (188): skipping input stream 0 0 0x0 message has been coming up. On testing with breakpoints, both soundname and url have expected values.
Does anybody have any idea as to how to fix this?
Move your instance of AVAudioPlayer outside of the method. If you do this it will keep player around long enough to play the sound. The reason it's not working is because it trashes the player instance after the method is complete because it thinks you are done using it.
class UIAlphabetController: UIViewController, AVAudioPlayerDelegate {
var player: AVAudioPlayer?
#IBAction func PlaySound(Sender: UIButton) {
let soundname:String = Sender.currentTitle!
let url = Bundle.main.url(forResource: soundname, withExtension: "mp3")
do {
player = try AVAudioPlayer(contentsOf: url!)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error { print(error.localizedDescription) }
}
}
I would like to be able to play audio on pressing a button a maximum of twice. After pressing the button twice it should no longer play audio even when pressed. I have this code currently, but it doesn't work and I am unsure where I am going wrong:
var soundFileURLRef: NSURL!
var audioPlayer = AVAudioPlayer?()
var audioCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
// setup for audio
let playObjects = NSBundle.mainBundle().URLForResource("mathsinfo", withExtension: "mp3")
self.soundFileURLRef = playObjects
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: soundFileURLRef)
} catch _ {
audioPlayer = nil
}
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
}
//function for counting times audio is played
func countAudio() {
if ((audioPlayer?.play()) != nil) {
++audioCounter
}
}
//MARK: Actions
//action for button playing the audio
#IBAction func playMathsQuestion(sender: AnyObject) {
countAudio()
if audioCounter < 2 {
audioPlayer?.play()
}
}
The countAudio function in your code have the side effect of playing the sound asynchronously since you call audioPlayer?.play(). The audioCounter variable is only incremented when audioPlayer is nil. Try this version
var soundFileURLRef: NSURL!
var audioPlayer: AVAudioPlayer?
var audioCounter = 0
override func viewDidLoad() {
super.viewDidLoad()
// setup for audio
let playObjects = NSBundle.mainBundle().URLForResource("mathsinfo", withExtension: "mp3")
self.soundFileURLRef = playObjects
audioPlayer = try? AVAudioPlayer(contentsOfURL: soundFileURLRef)
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
}
//MARK: Actions
//action for button playing the audio
#IBAction func playMathsQuestion(sender: AnyObject) {
if (audioPlayer != nil) {
if (audioCounter < 2 && audioPlayer!.play()) {
++audioCounter
}
}
}
You can see that here we first check to ensure the audioPlayer is not nil. After this we only do the audioPlayer!.play() call when audioCounter < 2. The call to play() returns a boolean that is true when the audio is successfully queued for play (see the documentation), and in that case we increment audioCounter.
I also simplified the initialization of audioPlayer by using the try? version of try which returns nil when the callee throws.