Add file to Swift Playground on Mac

Hello, does anyone know how can I add a sound file to my Swift playground on Mac? I want to use the code like below:

Code Block swiftSKAction.playSoundFileNamed("file_name", waitForCompletion: false)


Thank you so much! :)
Are you using Xcode or the Swift Playgrounds app to run your playground?

Did you add the soundFile to the project resources ?
If you're using the Swift Playgrounds app on Mac, you can open the Library popover by clicking the "+" button in the top-right-hand corner of the screen, then switch to the Files tab (the third one). You can then insert any files you'd like to have access to while your playground is running.
If you're using the Swift Playgrounds app (for Mac and iPad)...

You need to first add a sound file to the project:
  1. Press the “+” icon

  2. Tap the paper icon

  3. Tap Insert From…

  4. Insert a sound file from the Files app

Then, if you're using SwiftUI:
Code Block import AVFoundationimport SwiftUIimport PlaygroundSupportstruct SwiftUIAudioPlayerView: View {				/// the audio player that will play your audio file. Can't be a local variable.		/// Must be a `@State` property because we need to modify it later		@State var audioPlayer: AVAudioPlayer?				var body: some View {				Button(action: {						self.playAudio() /// play audio when tapped				}) {						Text("Play Audio!") /// what the button looks like				}		}				func playAudio() { /// function to play audio								/// the URL of the audio file.				/// forResource = name of the file. 				/// withExtension = extension, usually "mp3"				if let audioURL = Bundle.main.url(forResource: "slow-spring-board", withExtension: "mp3") {						do {								try self.audioPlayer = AVAudioPlayer(contentsOf: audioURL) /// make the audio player								self.audioPlayer?.numberOfLoops = 0 /// Number of times to loop the audio								self.audioPlayer?.play() /// start playing														} catch {								print("Couldn't play audio. Error: \(error)")						}										} else {						print("No audio file found")				}		}}let swiftuiAudioPlayerView = SwiftUIAudioPlayerView()PlaygroundPage.current.setLiveView(swiftuiAudioPlayerView)


...or UIKit:
Code Block import AVFoundationimport SwiftUIimport PlaygroundSupportclass UIKitAudioPlayerView: UIView {				/// the audio player that will play your audio file. Can't be a local variable.		var audioPlayer: AVAudioPlayer?				/// so we only make and connect the button once		var hasLoadedView = false		var audioButton = UIButton()				/// the view is guarenteed to be loaded at `layoutSubviews()` 		override func layoutSubviews() {				super.layoutSubviews()								if !hasLoadedView {						hasLoadedView = true												audioButton.setTitle("Play Audio!", for: .normal)						audioButton.setTitleColor(UIColor.blue, for: .normal)						backgroundColor = .white												addSubview(audioButton) /// add the button to the view						audioButton.addTarget(self, action: #selector(playAudio), for: .touchUpInside)				}								/// positioning (center-align the button)				let middleXOfView = bounds.width / 2				let middleYOfView = bounds.height / 2				let buttonFrame = CGRect(x: middleXOfView - 60, y: middleYOfView - 20, width: 120, height: 40)				audioButton.frame = buttonFrame		}				@objc func playAudio() { /// function to play audio								/// the URL of the audio file.				/// forResource = name of the file. 				/// withExtension = extension, usually "mp3"				if let audioURL = Bundle.main.url(forResource: "slow-spring-board", withExtension: "mp3") {						do {								try self.audioPlayer = AVAudioPlayer(contentsOf: audioURL) /// make the audio player								self.audioPlayer?.numberOfLoops = 0 /// Number of times to loop the audio								self.audioPlayer?.play() /// start playing														} catch {								print("Couldn't play audio. Error: \(error)")						}										} else {						print("No audio file found")				}		}}let uikitAudioPlayerView = UIKitAudioPlayerView()PlaygroundPage.current.setLiveView(uikitAudioPlayerView)


Replace "slow-spring-board" with the name of your sound file.

The trick here is to use Bundle.main.url(forResource:withExtension:). You MUST do this. If you try adding a file literal via the “+” button (by tapping an already-imported file), it won’t work.

- from a Medium article I'm writing on this topic (not published yet)


Add file to Swift Playground on Mac
 
 
Q