swift play audio file

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
swift play audio file from documentpicker swift play audio from local file swift play audio from file swift play audio from time swift run code while audio is playing how to play audio from internet in swift how to play recorded audio in swift swift play sound swift play audio how to play sound using swift play audio from url swift play a mp3 file swift how i can play sound swift play audio in swift 5 swift play audio using url play audio file in swift how to play audio swift swift new AVPlayer for new audio file? play audio file frim server swift play sound in ios swift swift play audio file from url swift play audio files play audio from document directory in ios swift play audio file offline swift play sound in swift play audio in swift play audio using swift 5 how to play audio using swift 5 play sound swift how to play audio in swift how to play audio in swift 5 swift play av audio file how to implement audio in swift how to play sound in swift play some sound swift audio player in swift ios AVAudioSequencer swift play mp3 sample play a sound on ios swift avfoundation play sound Play sounds using AVAudioPlayer iOS avaudioplayer swift 5 play audio swift avfoundation swift playing an audio file play an audio file swift 5 how to play sound in swift 5 add sound file in swift how to add mp3 into swift 4 how to play audio in swift 4 how to get the music playing swift 5 how to get music playing swift 5 play sound in swift 5 play wav file swift 5 How to play sound effects swift how to play sounds swift jquery play audio android swift sound AVFoundation play music swift swift 5 play sound how to add sound in swift playing a sound in swift swift play audio file
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