play sound swift stack overflow

import AVFoundation

var player: AVAudioPlayer?

func playSound() {
    guard let url = Bundle.main.url(forResource: "soundName", withExtension: "mp3") else { return }

    do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)            
        try AVAudioSession.sharedInstance().setActive(true)

        /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/
        player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)

        /* iOS 10 and earlier require the following line:
        player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3) */

        guard let player = player else { return }

        player.play()

    } catch let error {
        print(error.localizedDescription)
    }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
play sound with bundle swift stack overflow site:stackoverflow.com play sound swift stack overflow site:stackoverflow.com play different sounds swift stack overflow how to add sound swift stack overflow swiftui popover delay audio sound swift stack overflow play sound swift 5 stackoverflow play sound swift 5 stack overflow play music in swift 5 play .wav in swift swift play audio from list of numbers play audio in swift how to play sound in xcode How to play Audio music in swift how to add sound in swift play song in swift 5 swift add sound effect play sound swift ui stack overflow audio play music in swift 5 playing sound swift stack overflow swift play audio play music swift soundplay swift stack overflow how to play mp3 in ios swift how to play a sound swift how to play audio in ios swift URL of audio file to analyze swift how to make sound waves from m4a url in swift swift 5 play sound swift try AV audio play mp3 in swift swift play sound play a wav file ios swift ios swift play tone how to play sound on swift playsound swift stack overflow swift play sound switch how to add sound to app swift type(of: ) audio file swift 5 swift play sound stackoverflow add sound to swift stack overflow play a wav file swift play music swift stack overflow ios swift play sound play sound swift func playsound swift Avfoundation stackoverflow play sound in swift play sound swift stackoverflow how to play a sound in swift 5 how to play a sound in swift play local sound swift how to play sound swift stack overflow play sound swift stack overflow
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source