How to add sound in the app (swift)

This time, I will explain how to make a sound with the app. Recently I made an original application with rails, so I am studying mainly on swift.


import AVFoundation
//Need the above description
class calcViewController: UIViewController {
override func viewDidLoad() {
        // Do any additional setup after loading the view.


First, we will introduce a framework for producing sound with import AVFoundation. In addition to this framework, there are many frameworks released by Apple Inc., so please introduce them according to what you want to make. Here is a list of frameworks.

Next, let's put the music in the file. Please make it .mp3 in advance. Then put it in the file you are making in xcode. This time, the music to be put in the file will be tentatively named "correct.mp3" and "incorrect.mp3".


var player:AVAudioPlayer?
func playSound(name:String){
  let path = Bundle.main.bundleURL.appendingPathComponent(name + ".mp3")
  do {
      player = try AVAudioPlayer(contentsOf: path,fileTypeHint: nil)

We will instantiate it with var player: AVAudioPlayer? So that it can be used.

let path = Bundle.main.bundleURL.appendingPathComponent(name + ".mp3")

I put information in a constant called path. It is moving to find the imported music file with "Bundle.main.bundleURl" and reading the music file with "appendingPathComponent". The name of (name + ".mp3") takes an argument.

do {} catch {} is called do-catch, and it is a statement that handles exceptions. Call the instance with try and put path in contrastOf.


func setbutton{
  answer = 1
  if answer == 1{
   //When answer is 1
     playSound(name: "correct")
   //When answer is other than 1
     playSound(name: "incorrect")

Music is played by writing playSound (name: "name of music file"). Did you all do that?

Recommended Posts

How to add sound in the app (swift)
[Swift] How to link the app with Firebase
[Swift] Use UserDefaults to save data in the app
How to add the delete function
How to transition from the [Swift5] app to the iPhone settings screen
How to add the same Indexes in a nested array
Shorten the UUID to base64 in Swift.
How to change app name in rails
How to add jar file in ScalaIDE
[Swift] How to fix Label in UIPickerView
[Swift] How to implement the countdown function
How to get the date in java
How to overwrite Firebase data in Swift
[Swift] How to display when the quiz app answers correctly [Beginner]
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
How to check the logs in the Docker container
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add a classpath in Spring Boot
How to conditionally add html.erb class in Rails
[Swift] How to implement the fade-in / out function
Add classpath: to the path specified in spring.datasource.schema
How to execute tasks in parallel in Swift in Swift Package
[Swift] How to set an image in the background without using UIImageView.
How to build the simplest blockchain in Ruby
[Rails] How to apply the CSS used in the main app with Administrate
How to check Rails commands in the terminal
[Swift] How to get the number of elements in an array (super basic)
How to get the ID of a user authenticated with Firebase in Swift
[swift5] How to transition from the app to an external site by specifying the URL
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
How to implement UICollectionView in Swift with code only
How to set the display time to Japan time in Rails
[Java] How to omit the private constructor in Lombok
How to run the SpringBoot app as a service
Organized how to interact with the JDK in stages
How to specify the resource path in HTML import
How to debug the generated jar file in Eclipse
[Swift] How to get the document ID of Firebase
[Rails] How to display an image in the view
How to add ActionText function
[Swift] How to use UserDefaults
How to use Swift UIScrollView
4 Add println to the interpreter
How to add HDD to Ubuntu
[Swift] How to simply describe dismiss that was not taught in the introductory book
How to check if the characters entered in the Swift Text Field are email addresses
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
How to get the class name / method name running in Java
How to correctly check the local HTML file in the browser
How to use the getter / setter method (in object orientation)
How to set chrony when the time shifts in CentOS7
How to connect the strings in the List separated by commas
How to create a placeholder part to use in the IN clause
How to retrieve the hash value in an array in Ruby
How to implement infinite scrolling (page nate) in Swift TableView
How to deploy jQuery in your Rails app using Webpacker
How to add elements without specifying the length of the array
Add a shadow to the Swift Button (and also the circle)
[Swift5] How to avoid applying dark mode (dark appearance) in code
How to develop and register a Sota app in Java