Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

ios - How to add sound files to your bundle in Xcode?

I am trying to make an application with sound. I have asked a question before how to add sound to an app in Xcode but every time I run the app, the console throws this error, MagicSound.wav does not exist in main bundle when writing my code I basically told the program to crash the application and write MagicSound.wav does not exist in main bundle if it comes to be true. My question is how do you add a file (specifically a sound file) to your bundle in Xcode. I always assumed putting a file in your assets is the same thing as putting it in your bundle.

Here is my code,

import UIKit
import AVFoundation

class ViewController: UIViewController {
    
    
    var magicSound: AVAudioPlayer = AVAudioPlayer()
    

    @IBOutlet var Answer: UILabel!
    
    var AnswerArray = ["Yes", "No", "Maybe", "Try Again", "Not Now", "No Doubt", "Yes Indeed", "Of course", "Definetley Not"]
    var chosenAnswer = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        if let magicFile = Bundle.main.path(forResource: "MagicSound", ofType: "wav") {
            _ = try? AVAudioPlayer(contentsOf: URL (fileURLWithPath: magicFile))
        }
        else {
            print( "MagicSound.wav does not exist in main bundle" )
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        
        if event?.subtype == motion {
            printAnswer()
            randomAnswer()
            animation()
            showingAnswerAnimation()
            magicSound.play()
        }
        
    }

If anybody could help that would be great!

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This answer is top of Google searching for 'How to add sound files to your bundle in Xcode?', so to help rookies like me, here is step by step how you add sounds to your bundle in Xcode. (Short answer, just drag them into the sidebar and check the 'Copy items if needed')

  1. Put the sounds in a folder (you don't have to but keeps them organized)

  2. Drag the folder of sounds into the sidebar in Xcode. enter image description here

  3. When you drag the folder in, it will give you a few options. Make sure 'Copy items if needed' is checked. It should be default. enter image description here

  4. You are done! Access your sound by getting the path and URL based on the file name.

Like this:

func playSaveSound(){
    let path = Bundle.main.path(forResource: "upward.wav", ofType: nil)!
    let url = URL(fileURLWithPath: path)

    do {
        //create your audioPlayer in your parent class as a property
        audioPlayer = try AVAudioPlayer(contentsOf: url)
        audioPlayer.play()
    } catch {
        print("couldn't load the file")
    }
}

This article has a nice little overview of how to play sounds.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...