I am trying to have a button that will play a sound when pushed.
The app I have has 6 buttons (with 6 different sounds). I only have one listed because I am trying to get one button to work correctly before doing the rest. I know the button is working (from the print command at the bottom), but it is not playing the sound.
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var player : AVAudioPlayer?
#IBAction func buttonOne(_ sender: Any) {
func playSound() {
let url = Bundle.main.url(forResource: "drinking", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}
}
print("Anakin: IT IS WORKING!")
}
}
Is it printing any error?
Can you try doing this?
let path = Bundle.main.path(forResource: "FILE NAME WITH EXTENSION", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
sound.play()
} catch {
// couldn't load file :(
}
Related
I am new at swift and Xcode, and When I am trying to play a sound, it gives me an error that I have no idea what is wrong.
This is the code:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttonC(_sender: UIButton) {
playSound()
}
func playSound() {
guard let path = Bundle.main.path(forResource: "C", ofType:"wav") else {
return }
let url = URL(fileURLWithPath: path)
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch let error {
print(error.localizedDescription)
}
}
}
And this is the error:
I will appreciate the help; thanks!
Probably you have renamed your IBAction method name, now it's different and it's connected to the previous name inside your storyboard.
Disconnect your action method and reconnect it appropriately.
Go to your storyboard, select button and then in connectionInspector (cmd + option + 6) delete your previous connection.
Then link your button to the #IBAction function correctly.
I hope it helps you
I have added background music to my app using the following code:-
Now I am trying to add audio controls to my app using the following methods, but after I click on another view controller and come back to the main view controller the play button is not working. Any help will be highly appreciated Thanks!
var toggleState = 1
var music: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
playMusic()
}
func playMusic() {
if let musicURL = Bundle.main.url(forResource: "Adding", withExtension: "mp3") {
if let audioPlayer = try? AVAudioPlayer(contentsOf: musicURL) {
music = audioPlayer
music.numberOfLoops = -1
music.play()
}
}
}
#IBAction func playButtonPressed(_ sender: Any) {
if (toggleState == 1) {
music.pause()
toggleState = 2
}
else {
music.play()
toggleState = 1
}
}
I want to make it so that a button can be clicked, which will start repeatedly playing a system sound over and over (without overlapping the sounds) until the button is clicked again - so essentially, the button will toggle the repeated playing of an audio services system sound on/off.
If anyone would know how to do this I would greatly appreciate it.
Thanks
To do this use an AVAudioplayer with infinite repeat, and then pause/play it when button is pressed.
First make a audioPlayer variable, outside of any function:
var audioPlayer : AVAudioPlayer!
Then initialise the audioPlayer inside of viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
guard let url = Bundle.main.path(forResource: "myOwnSound", ofType: "wav") else { //your own file name AND extension type of course
print("file not found")
return
}
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOf: URL(string: url)!)
audioPlayer!.prepareToPlay()
audioPlayer.numberOfLoops = -1
} catch let error as NSError {
print("error while initialising sound: \(error)")
}
}
Then inside a function linked to your button, in my case I named that function buttonPressed:
#IBAction func buttonPress(_ sender:AnyObject){
if audioPlayer.isPlaying {
audioPlayer.pause()
}
else {
audioPlayer.play()
}
}
You can use AVFoundation and AVAudioPlayer to manage music and
sound inside you application
Import following
import AVFoundation
Create a property of AVAudioPlayer to manage play/stop and add following code inside the scope of your music button
var player : AVAudioPlayer?
#IBAction func openListAction(_ sender: UIButton) {
// moveToGame()
if sender.isSelected {
sender.isSelected = false
player?.stop()
}
else {
sender.isSelected = true
let bundle = Bundle.init(for: self.classForCoder)
let path = bundle.path(forResource: "soundFileName", ofType : "soundFileExtension")! //.mp3, .caf etc
let url = URL(fileURLWithPath : path)
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch {
print ("Error to load music")
}
}
}
My final game over scene has an SKTransition back to the Main Menu. I am able to make a song play for the final game over scene, but I would like the song to continue into my Main Menu.
Here is a copy of the code I have at the moment.
import Foundation
import SpriteKit
import AVFoundation
class SceneThree: SKScene {
var game = SKSpriteNode()
var new: AVAudioPlayer?
override func didMove(to view: SKView) {
self.backgroundColor = SKColor.black
playSound()
}
func playSound() {
let url = Bundle.main.url(forResource: "new", withExtension: "caf")!
do {
new = try AVAudioPlayer(contentsOf: url)
guard let new = new else { return }
new.prepareToPlay()
new.play()
} catch let error {
print(error.localizedDescription)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let gameScene = GameScene(fileNamed: "GameScene")
gameScene?.scaleMode = .aspectFill
self.view?.presentScene(gameScene!, transition: SKTransition.fade(withDuration: 4.5))
}
When you change scene the old one will get destroyed, which includes your AVPlayer property.
You can create a helper class for your music to avoid this.
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
I'm trying to add sound to an iOS app that I am writing. I keep getting the error message
"Incorrect argument label in call (have 'contentsOfURL:error:', expected "contentsOfURL:fileTypeHint:')".
I have tried tinkering with it in several ways, and I can't get it to build. Here is my code:
import UIKit import AVFoundation
class ViewController: UIViewController {
var Setup = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("DMSetup", ofType: "mp3")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
audioPlayer = AVAudioPlayer(contentsOfURL: Setup, error: nil)
audioPlayer.prepareToPlay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func PlayKeySetup(sender: UIButton) {
audioPlayer.play()
}
}
Any suggestions? I'm a newbie at this, so I'm sure I'm missing something obvious.
xCode 7.3.1, OSX 10.11.4
Thanks.
Try this code:
var audioPlayer: AVAudioPlayer!
let path = NSBundle.mainBundle().pathForResource("yourfilename", ofType: "format")
let CREATE_ANY_VARIABLE = NSURL(fileURLWithPath: path!)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: CREATE_ANY_VARIABLE)
} catch let error as NSError {
print(error.debugDescription)
}
audioPlayer.prepareToPlay()
and then you can play anywhere in the function!
the problem in your code is the name of your var Setup is overlapping with another Setup method form the AVFoundation
Usually using uppercase to the properties name it's considered as a bad attitude, you should use setup
Try this code:
let setupUrl = NSBundle.mainBundle().URLForResource("DMSetup", withExtension: "mp3")
if (setupUrl == nil) {
print("Could not find file: DMSetup.mp3")
return
}
do { audioPlayer = try AVAudioPlayer(contentsOfURL: Setup!, fileTypeHint: nil) }
catch let error as NSError { print(error.description) }
if let player = audioPlayer {
player.prepareToPlay()
}