简体   繁体   English

如何在 Xcode 中将声音文件添加到您的包中?

[英]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.我在如何在 Xcode 中向应用程序添加声音之前问过一个问题,但是每次运行应用程序时,控制台都会抛出此错误,编写代码时MagicSound.wav does not exist in main bundle我基本上告诉程序崩溃application 和 write MagicSound.wav does not exist in main bundle如果它成为真的MagicSound.wav does not exist in main bundle My question is how do you add a file (specifically a sound file) to your bundle in Xcode.我的问题是如何将文件(特别是声音文件)添加到 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.谢谢。

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.这个答案是 Google 搜索“如何在 Xcode 中将声音文件添加到您的包中?”的顶部,因此为了帮助像我这样的新手,这里将逐步介绍如何在 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.将声音文件夹拖到 Xcode 的侧边栏中。 在此处输入图片说明

  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.应该是默认的。 在此处输入图片说明

  4. You are done!你完成了! Access your sound by getting the path and URL based on the file name.通过根据文件名获取路径和 URL 来访问您的声音。

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. 这篇文章对如何播放声音有一个很好的概述。

There is an extra step here that XCode is doing implicitly, and it's possible to have your stuff configured in such a way that it doesn't get done.这里有一个额外的步骤,XCode 是隐式执行的,并且可以将您的东西配置为无法完成的方式。 You need to make sure that the wav file is in the list of items under your target's Build Phases -> Copy Bundle Resources setting.您需要确保 wav 文件位于目标的 Build Phases -> Copy Bundle Resources 设置下的项目列表中。

If you just have a main target for your app and nothing fancy, then this should be done automatically when you add the file to your folder in XCode.如果您的应用程序只有一个主要目标并且没什么特别的,那么当您将文件添加到 XCode 中的文件夹时,这应该会自动完成。 But I have a screwy setup where it's not, and I had to dig to find it.但是我有一个奇怪的设置,它不是,我不得不挖掘才能找到它。

Follow these steps:按着这些次序:

  1. add the sound files in your project by dragging them to the Xcode sidebar通过将声音文件拖到 Xcode 侧边栏来添加项目中的声音文件
  2. a dialog will appear - check on Copy items needed and add files.将出现一个对话框 - 检查Copy items needed并添加文件。
  3. to test if files were added properly, in Xcode right-click on any of the added files and click on show in Finder .要测试文件是否已正确添加,请在 Xcode 中右键单击任何添加的文件,然后单击show in Finder If the file exists then on runtime it will not throw an error.如果文件存在,那么在运行时它不会抛出错误。

Another thing to add, in addition to the other answers here is that when you import, you need to make sure it's copied to your app, in "Add to targets".除了这里的其他答案之外,要添加的另一件事是,当您导入时,您需要确保将其复制到您的应用程序中,位于“添加到目标”中。 This accomplishes the sound file being in "the list of items under your target's Build Phases -> Copy Bundle Resources setting."这完成了声音文件位于“目标的构建阶段-> 复制捆绑资源设置下的项目列表”中。

You need to get all of these things right or it won't be in the Bundle.您需要正确处理所有这些事情,否则它就不会出现在捆绑包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM