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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46231153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:14:20  来源:igfitidea点击:

How to add sound files to your bundle in Xcode?

iosxcodefilebundle

提问by S.Bhatia

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 bundlewhen writing my code I basically told the program to crash the application and write MagicSound.wav does not exist in main bundleif 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.

我正在尝试制作一个有声音的应用程序。我在如何在 Xcode 中向应用程序添加声音之前问过一个问题,但每次运行应用程序时,控制台都会抛出此错误,MagicSound.wav does not exist in main bundle在编写代码时,我基本上告诉程序使应用程序崩溃并编写MagicSound.wav does not exist in main bundle它是否正确. 我的问题是如何将文件(特别是声音文件)添加到 Xcode 中的包中。我一直认为将文件放入您的资产中与将其放入您的包中是一回事。

Here is my code,

这是我的代码,

`import UIKit import AVFoundation

`导入 UIKit 导入 AVFoundation

class ViewController: UIViewController {

类视图控制器: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.

谢谢。

回答by Joshua Dance

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')

这个答案是 Google 搜索“如何在 Xcode 中将声音文件添加到您的包中?”的顶部,因此为了帮助像我这样的新手,这里将逐步介绍如何在 Xcode 中将声音添加到您的包中。(简短的回答,只需将它们拖到侧边栏中并选中“如果需要复制项目”)

  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.

  1. 将声音放在一个文件夹中(你不必,但要保持它们的组织性)

  2. 将声音文件夹拖到 Xcode 的侧边栏中。 在此处输入图片说明

  3. 当您将文件夹拖入时,它会给您几个选项。确保选中“如果需要,请复制项目”。应该是默认的。 在此处输入图片说明

  4. 你完成了!通过根据文件名获取路径和 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 articlehas a nice little overview of how to play sounds.

这篇文章对如何播放声音有一个很好的概述。

回答by user435779

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. 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.

这里有一个额外的步骤,XCode 是隐式执行的,并且可以将您的东西配置为无法完成的方式。您需要确保 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. But I have a screwy setup where it's not, and I had to dig to find it.

如果您的应用程序只有一个主要目标并且没有什么特别的,那么当您将文件添加到 XCode 中的文件夹时,这应该会自动完成。但是我有一个奇怪的设置,它不是,我不得不挖掘才能找到它。

回答by Parkhya Dev

Follow these steps:

按着这些次序:

  1. When you try to add the sound file in your project, then during adding make a check on Copy items needed and add file.
  2. Now from Xcode click on that file and right click show in Finder option appears click on that and see is you file exist. If it is exist then on runtime it will not throws error.
  1. 当您尝试在项目中添加声音文件时,请在添加过程中检查需要的复制项目并添加文件。
  2. 现在从 Xcode 单击该文件并右键单击显示在 Finder 选项中出现单击该文件并查看您的文件是否存在。如果它存在,那么在运行时它不会抛出错误。

回答by user3120572

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.

您需要正确处理所有这些事情,否则它就不会出现在捆绑包中。