Could someone please help and tell me what the code would be to insert sound into my xcode 6 swift project and have it playing in the background
Asked
Active
Viewed 6,698 times
1 Answers
5
For OS X, to get sounds to play you can either use NSSound or for more advanced things Core Audio.
With NSSound (the easiest way) you would put a sound file in your assets library, and then do:
var sound = NSSound(named: "name_of_sound")
sound.play()
For iOS try the following code:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound_name", ofType: "wav"))
println(alertSound)
// Removed deprecated use of AVAudioSessionDelegate protocol
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}
and it seems to be a good idea in general to have the audio player as a class member

James Alvarez
- 7,159
- 6
- 31
- 46
-
it doesn't work, I get the error - missing argument for parameter 'size' in call for the first line of code and for the second I get the error - expected declaration – HappyHarpreet Aug 10 '14 at 09:59
-
There is nothing wrong with the code - it should work perfectly I have used it a lot. What file format is your sound, and where have you put it in your project? e.g. if you have a wav sound 'alrighty.wav' you add the file to your project and then use the code var sound = NSSound(named: "alrighty.wav") – James Alvarez Aug 10 '14 at 10:09
-
I am using a .wav file format and I have put into my project but I still get the same error, don't I need to import any module like i have done import SpirteKit but dont I need to import any audio module – HappyHarpreet Aug 10 '14 at 10:43
-
yes sorry, I forgot to mention that. I've just tried the code you gave me but now I get the error - 'GameScene.Type' does not have member named 'sound' – HappyHarpreet Aug 10 '14 at 11:13
-
and i also get the error- expected declaration for the 3 line of code – HappyHarpreet Aug 10 '14 at 11:14
-
The code is correct, it's probably where you've put it... Here is a more extensive answer which might help? http://stackoverflow.com/questions/24386766/ios-sound-not-playing-in-swift?rq=1 – James Alvarez Aug 10 '14 at 11:17
-
If I was to put the code i have atm into the question could be able to tell me where to put it please, I'm sorry I don't understand how to do this. – HappyHarpreet Aug 10 '14 at 11:23
-
Don't worry, a better snippet is now in the question, so hopefully it will be clearer. You can add this as a view controller, and it will work... – James Alvarez Aug 10 '14 at 11:26
-
All the code seems to be fine expect for the override func viewDidLoad() { because it give me the error - invalid redeclaration of 'viewdidload()' – HappyHarpreet Aug 10 '14 at 11:35
-
James can you please help? – HappyHarpreet Aug 10 '14 at 20:39
-
Keep trying it is correct!! Start a new project and add this view and see that it works plus please accept the answer! – James Alvarez Aug 11 '14 at 16:40