Completely delete SKSpriteNode variable - swift

I have a subclass of SKSpriteNode called 'backgroundMusic' that consists of a SKSpriteNode and a AVAudioPlayer file. The objective is to completely delete 'backgroundMusic' after i instantiate it. I try to do :
backgroundMusic.removeFromParent()
but it only removes the SKSpriteNode and not the AVAudioPlayer.
The main issue is that inside of this subclass, I call a bunch of functions within other functions, and when i try to empty the subclass by:
backgroundMusic = nil
the process of calling all the functions still is occurring and causes issues when i re-instantiate it. What i believe will work is if I delete 'backgroundMusic' completely, which will stop the function calling process, and then later re-instantiate it when i need to, it should work fine with no issues. How can I do this?
EDIT I tried:
self.delete(backgroundMusic)
and it crashed the application. Should I use this? If so how?

This happened because you havent configure Audio Session
Some code for playing:
import AVFoundation
var audioPlayer = AVAudioPlayer()
func playAudio() {
// Set the sound file name & extension
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav")!)
// Preperation
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
try! AVAudioSession.sharedInstance().setActive(true)
// Play the sound
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print("there is \(error)")
}
}
Details from the docs:
AVAudioSessionCategoryOptionMixWithOthers
Mixes audio from this session with audio from other active sessions.
Valid only if the session category is
AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryPlayback.
(Implicit if the session category is AVAudioSessionCategoryAmbient.)
If you activate your session while using this option, your app’s audio
will not interrupt audio from other apps (such as the Music app). If
not using this option (or a category that is implicitly mixable),
activating your session will interrupt other nonmixable sessions.
To stop you can do:
do {
try AVAudioSession.sharedInstance().setActive(false)
} catch let error as NSError {
print(error.localizedDescription)
}

Related

How to play sounds, like the ringing beep, while using Callkit?

I am trying to implement VoIP using webRTC and Callkit. The audio works perfectly fine during the call, but I would like to play sounds to the user while the user is initiating a call (an outgoing call).
When the user initiates a call and waits for the recipient to answer, i would like to play the waiting beep sound (long beeps). I can manage to play the sound without using Callkit but when I inform Callkit about an outgoing call it somehow cancels the audio. My assumption is it does this because it IOS silences audio when a call is beging made.
So my question is, how can i playback an mp3 file when Callkit is active. Or is this waiting sound somehow integrated in Callkit or WebRTC?
I messed around with different categories for the audiosession but no luck so far. See below a summary of my current code.
public var audioPlayer: AVAudioPlayer?
private init() {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "dialring", ofType: "mp3")!))
audioPlayer!.prepareToPlay()
audioPlayer!.numberOfLoops = -1 //loop
} catch {
print(error.localizedDescription)
}
}
func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
configureAudioSession()
audioPlayer?.play()
}
func configureAudioSession() {
print("Configuring audio session")
let session = AVAudioSession.sharedInstance()
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.mixWithOthers])
try session.setMode(AVAudioSession.Mode.voiceChat)
} catch (let error) {
print("Error while configuring audio session: \(error)")
}
}
If anyone could point me in the right direction I would be grateful.
EDIT: I have background mode for audio enabled.
Threw around the structure a bit and now it works. I am not exactly sure what changed thought. If people face the same issue.
- Make sure you keep a strong reference to the audioplayer.
- Make sure the mode is either in .playback or .playAndRecord

AVAudioPlayer does not play audio file - swift

I know it seems quite straightforward, but in a weird way AVAudioPlayer does not play audio file. I have this code. It is not go through catch and still sound does not play. I've checked on physical device, simulator and also checked sound volume. But still I can't hear sound.
//in ViewController as a property
let soundEffect: AVAudioPlayer!
//in viewDidLoad
let path = Bundle.main.path(forResource: "success.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
soundEffect = try AVAudioPlayer(contentsOf: url)
soundEffect.prepareToPlay()
soundEffect.play()
} catch let error as NSError {
print(error.description)
}
I also tried let path = Bundle.main.path(forResource: "success", ofType:"mp3")! but it didn't make any difference.
Try making your AVAudioPlayer an instance variable. otherwise soundEffect is deallocated at the end of the do loop.

Cannot stop background music from within Game Scenes, Swift 3/Spritekit

On XCODE 8/Swift 3 and Spritekit, i am playing background music (a 5 minute song), calling it from GameViewController's ViewDidLoad (from the parent of all the scenes, not from a specific GameScene), as I want it to play throughout scene changes without stopping. This happens without a problem.
But my problem is, how do i stop the background music at will, when I am inside a scene? Say when user gets to a specific score on the game on the 3rd scene? As i cannot access the methods of the parent file. Here is the code I used to call the music to play:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var audioPlayer = AVAudioPlayer()
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "music", ofType: "mp3")!))
audioPlayer.prepareToPlay()
} catch {
print (error)
}
audioPlayer.play()
Many thanks for any help
Why not create a music helper class that you can access from anywhere. Either the singleton way or a class with static methods. This should also make your code cleaner and easier to manage.
I would also split the setup method and the play method so that you do not set up the player each time you play the file.
e.g Singleton
class MusicManager {
static let shared = MusicManager()
var audioPlayer = AVAudioPlayer()
private init() { } // private singleton init
func setup() {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "music", ofType: "mp3")!))
audioPlayer.prepareToPlay()
} catch {
print (error)
}
}
func play() {
audioPlayer.play()
}
func stop() {
audioPlayer.stop()
audioPlayer.currentTime = 0 // I usually reset the song when I stop it. To pause it create another method and call the pause() method on the audioPlayer.
audioPlayer.prepareToPlay()
}
}
When your project launches just call the setup method
MusicManager.shared.setup()
Than from any where in your project you can say
MusicManager.shared.play()
to play the music.
To than stop it just call the stop method
MusicManager.shared.stop()
For a more feature rich example with multiple tracks check out my helper on GitHub
https://github.com/crashoverride777/SwiftyMusic
Hope this helps

How to play the same sound overlapping with AVAudioPlayer?

This code plays the sound when the button is tapped but cancels the previous if it is pressed again. I do not want this to happen I want the same sound to overlap when repeatedly pressed. I believe it might be due to using the same AVAudioPlayer as I have looked on the internet but I am new to swift and want to know how to create a new AVAudioPlayer everytime the method runs so the sounds overlap.
func playSound(sound:String){
// Set the sound file name & extension
let soundPath = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
do {
//Preperation
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch _{
}
do {
try AVAudioSession.sharedInstance().setActive(true)
}
catch _ {
}
//Play the sound
var error:NSError?
do{
audioPlayer = try AVAudioPlayer(contentsOfURL: soundPath)
}catch let error1 as NSError {
error = error1
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
To play two sounds simultaneously with AVAudioPlayer you just have to use a different player for each sound.
In my example I've declared two players, playerBoom and playerCrash, in the Viewcontroller, and I'm populating them with a sound to play via a function, then trigger the play at once:
import AVFoundation
class ViewController: UIViewController {
var playerBoom:AVAudioPlayer?
var playerCrash:AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
playerBoom = preparePlayerForSound(named: "sound1")
playerCrash = preparePlayerForSound(named: "sound2")
playerBoom?.prepareToPlay()
playerCrash?.prepareToPlay()
playerBoom?.play()
playerCrash?.play()
}
func preparePlayerForSound(named sound: String) -> AVAudioPlayer? {
do {
if let soundPath = NSBundle.mainBundle().pathForResource(sound, ofType: "mp3") {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
return try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: soundPath))
} else {
print("The file '\(sound).mp3' is not available")
}
} catch let error as NSError {
print(error)
}
return nil
}
}
It works very well but IMO is not suitable if you have many sounds to play. It's a perfectly valid solution for just a few ones, though.
This example is with two different sounds but of course the idea is exactly the same for two identic sounds.
I could not find a solution using just AVAudioPlayer.
Instead, I have found a solution to this problem with a library that is built on top of AVAudioPlayer.
The library allows same sounds to be played overlapped with each other.
https://github.com/adamcichy/SwiftySound

Why can't I playback sound files recorded by AVAudioRecorder?

This is how I defined myrecorder and myplayer
var myrecorder:AVAudioRecorder!
var myplayer : AVAudioPlayer!
This is the code to setup myrecorder in the viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
filepath = "\(getDocumentsDirectory())/001.caf"
print(filepath)
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)
//define settings
let recordSettings: [String: AnyObject] = [
AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),
AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue,
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 2,
]
myrecorder = try! AVAudioRecorder(URL: NSURL(fileURLWithPath: filepath), settings: recordSettings)
myrecorder.delegate = self
myrecorder.prepareToRecord()
}
And I have buttons to control record and stop and I got the file 001.caf. I can playback the sound file but I think the file is not good enough.
#IBAction func Play() {
try! myplayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: filepath))
myplayer.volume = 0.5
myplayer.play()
print("Playing...")
}
The playback is successful without any error or crash. With only one problem, it doesn't have any sound when playing.
I assume that I'd missed something in the recording phase but I couldn't locate it. Please help.
The issue is with the AVAudioPlayer that is instantiated in this line.
try! myplayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: filepath))
This instance myplayer will get deallocated as soon as the play function completes.
Instantiate the AVAudio player in another function, such as Viewdidload, so that it sticks around even after the control leaves the play function.
Ok, finally I found the problem.
YOU CAN'T USE AVAudioSessionCategoryPlayAndRecord.
When recording, you had to use AVAudioSessionCategoryRecord and when playback you use AVAudioSessionCategoryPlayback. And you set the session back and forth when playback and record.
AVAudioSessionCategoryPlayAndRecord is used when playback and recording are needed in the same time, like when you do voice chat...