如何在Xcode中将声音文件添加到包中? [英] How to add sound files to your bundle in Xcode?

查看:209
本文介绍了如何在Xcode中将声音文件添加到包中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个有声音的应用程序。在向Xcode中的应用添加声音之前,我曾问过一个问题,但是每次运行该应用时,控制台都会抛出此错误,主捆绑包中不存在 MagicSound.wav 在编写代码时,我基本上告诉程序要崩溃的应用程序并编写 MagicSound.wav在主捆绑包中不存在,如果它是真的。我的问题是如何在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.

这是我的代码,

`导入UIKit
导入AVFoundation

`import UIKit import AVFoundation

class ViewController:UIViewController {

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!

谢谢。

推荐答案

此答案是Google搜索如何在Xcode中将声音文件添加到您的包中?的顶部。为了帮助像我这样的新手,请按以下步骤逐步在Xcode中向包中添加声音。 (简短的答案,只需将其拖到侧边栏中,然后选中如果需要,复制项目即可)。

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. 将声音放入文件夹(您不必但要使它们井井有条)

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

将声音文件夹拖到Xcode的侧栏中。

Drag the folder of sounds into the sidebar in Xcode.

将文件夹拖入其中时,它将为您提供一些选择。确保选中如果需要复制项目。它应该是默认值。

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.

您完成了!通过获取基于文件名的路径和URL来访问声音。

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

就像这样:

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.

这篇关于如何在Xcode中将声音文件添加到包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆