Code after 'return' will never be executed - ios

I'm in my first week of coding.
So far the youtube tutorial app I am making in swift is going perfectly apart from the above error message. Here is the code. I have rewritten this twice now exactly as they specify.
I am trying to set up sounds to play with the card game we are making, the sounds play when cards are shuffled, turned around, matched or incorrectly matched.
The error message is shown for "let soundURL" line of code, "Code after 'return' will never be executed".
Please help?
class SoundManager {
static var audioPlayer:AVAudioPlayer?
enum SoundEffect {
case flip
case shuffle
case match
case nomatch
}
static func playSound(_ effect:SoundEffect) {
var soundFilename = ""
// Determine which sound effect we want to play
//and set the appropriate file name
switch effect {
case .flip:
soundFilename = "cardflip"
case .shuffle:
soundFilename = "cardflip"
case .match:
soundFilename = "dingcorrect"
case.nomatch:
soundFilename = "dingwrong"
}
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
}
}
}

I believe you want your code to look something like this:
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
}
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
If there is a guard statement with return inside its brackets, you do not want to put any code beneath the return and within the same brackets. None of the code below the return will execute. In my example, the following code is outside of the brackets, meaning it will execute so long as there is a value for bundlePath.

Code after 'return' will never be executed
var soundFilename = ""
// Determine which sound effect we want to play
//and set the appropriate file name
switch effect {
case .flip:
soundFilename = "cardflip"
case .shuffle:
soundFilename = "cardflip"
case .match:
soundFilename = "dingcorrect"
case.nomatch:
soundFilename = "dingwrong"
}
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
}
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
// }
}
I believe that you have misunderstood the guard syntax as it will execute the code when the condition is correct and will fall in else part when the condition is false. So therefore it will use return in else with no other code , So that the method will be terminated gracefully without giving any crash.
The return keyword is used to return something with the method execution or to exit from the method.
So you need to close the else part with “}” and remove one below catch block. Hope this helps.

Related

Play a sound file on repeat in Swift?

I have had success playing sounds by pressing a button, by using the following code. However, I'd like to press a button and have that sound play in a loop/infinitely. How is this acheived? I'd also like to acheive pause/play functionality as well. Thanks in advance.
#IBAction func keyPressed(_ sender: UIB`utton) {
playSound(soundName: sender.currentTitle!)
}
func playSound(soundName: String) { //
let url = Bundle.main.url(forResource: soundName, withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
} // End of Play Sound
By default AVAudioPlayer plays its audio from start to finish then stops, but we can control how many times to make it loop by setting its numberOfLoops property. For example, to make your audio play three times in total, you’d write this:
player.numberOfLoops = 3
if you want the infinite loop then use
player.numberOfLoops = -1
for e.g
func playSound(soundName: String) { //
let url = Bundle.main.url(forResource: soundName, withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.numberOfLoops = -1 // set your count here
player.play()
} // End of Play Sound
var audioPlayer: AVAudioPlayer?
func startBackgroundMusic() {
if let bundle = Bundle.main.path(forResource: "Guru_Nanak_Sahib_Ji", ofType: "mp3") {
let backgroundMusic = NSURL(fileURLWithPath: bundle)
do {
audioPlayer = try AVAudioPlayer(contentsOf:backgroundMusic as URL)
guard let audioPlayer = audioPlayer else { return }
audioPlayer.numberOfLoops = -1 // for infinite times
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
}
You can retain the player. Then in the player's completion delegate callback, start playing again. Or stop the player at any time, since you've retained a reference to it.

Using AVAudioPlayer with Dynamic URL in Swift 3 causing Thread Errors

I am new to Swift and making an audio app using AVAudioPlayer. I am using a remote URL mp3 file for the audio, and this works when it's static.
For my use case, I want to pull a URL for an mp3 file from a JSON array and then pass it into the AVAudioPlayer to run.
If I move the AVAudioPlayer block into the ViewDidLoad and make the mp3 file a static URL, it will run fine.
Then, when I move this code into my block that extracts an mp3 url from JSON, I can print the URL successfully. But when I pass it into my audio player, problems arise. Here's the code.
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://www.example.com/example.json")
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
guard let data = data, error == nil else { return }
let json: Any?
do{
json = try JSONSerialization.jsonObject(with: data, options: [])
}
catch{
return
}
guard let data_list = json as? [[String:Any]] else {
return
}
if let foo = data_list.first(where: {$0["episode"] as? String == "Example Preview"}) {
self.audiotest = (foo["audio"] as? String)!
print(self.audiotest) // this prints
// where i'm passing it into the audio player
if let audioUrl = URL(string: self.audiotest) {
// then lets create your document folder url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// lets create your destination file url
let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
//let url = Bundle.main.url(forResource: destinationUrl, withExtension: "mp3")!
do {
audioPlayer = try AVAudioPlayer(contentsOf: destinationUrl)
} catch let error {
print(error.localizedDescription)
}
} // end player
// ....
Specifically, I get an error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value when clicking a play button IBAction that is connected to the audio player. Finally, that action function looks like this:
#IBAction func playPod(_ sender: Any) {
audioPlayer.play()
}
Do you know where I'm going wrong? I'm confused as to why I can't print the URL and also get a response that the URL is nil in the same block, but maybe that's an asynchronous thing.
The problem is that you didn't save the mp3 file to documents and trying to play it
this line
audioPlayer = try AVAudioPlayer(contentsOf: destinationUrl)
assumes that there is a saved mp3 file in that path , but acutally there is no files you appended the audio extension on the fly
besides for steaming audio from a remote server, use AVPlayer instead of AVAudioPLayer.
AVPlayer Documentation
Also try this with urls parsed from json
var urlStr = (foo["audio"] as? String)!
self.audiotest = urlStr.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

How to play sound note (.mid file) on iOS?

I want to make piano player in which I want to play predefined notes which are some .mid files. Here is my code which is not working.
let soundPath: String? = Bundle.main.path(forResource: "0_100", ofType: "mid")
let midiFile:URL = URL(fileURLWithPath: soundPath ?? "")
var midiPlayer: AVMIDIPlayer?
do {
try midiPlayer = AVMIDIPlayer(contentsOf: midiFile, soundBankURL: nil)
midiPlayer?.prepareToPlay()
midiPlayer?.play {
print("finished playing")
}
} catch {
print("could not create MIDI player")
}
soundBankURL is missing in the following:
try midiPlayer = AVMIDIPlayer(contentsOf: midiFile, soundBankURL: nil)
It's required according to the doc: https://developer.apple.com/documentation/avfoundation/avmidiplayer/1389225-init
Important
For macOS the bankURL can be set to nil to use the default sound bank. However, iOS must always refer to a valid bank file.
A soundBank can be a .sf2 file for instance.

Play/Pause Audio Button Swift

I'm new to swift and can't seem to figure this out. I am trying to have one button act as both a play button which loops audio and as a pause button if clicked again. I've tried using an if/else statement but I keep getting this error "fatal error: unexpectedly found nil while unwrapping an Optional value"
The code works without the if/else statement, however, in that case it loops indefinitely without a way to pause it.
Any suggestions?
var hihat16: AVAudioPlayer!
#IBAction func hat16(_ sender: Any) {
if hihat16.isPlaying == false {
let path = Bundle.main.path(forResource: "Hat 017-1.aif", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
hihat16 = sound
sound.play()
hihat16.numberOfLoops = -1
} catch {
// couldn't load file :(
}
}else{ hihat16.stop()}
Picture of the code and error message :
Try this.
Do it in your viewDidLoad
let path = Bundle.main.path(forResource: "Hat 017-1.aif", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
hihat16 = try AVAudioPlayer(contentsOf: url)
hihat16.numberOfLoops = -1
} catch {}
Then in your #IBAction you can do this.
if !hihat16.isPlaying {
hithat16.play()
}
else{ hithat.stop()}

AVAudioSession/AVQueuePlayer/AVAudioPlayer in the Background Swift

Problem: I am trying to get my audio to continue to play even when the app is in the background. The desired result would be that the audio continues to play even when: the home button is pressed, the phone sleeps/is locked, and to be able to mix with the other sounds (native music app, etc). I have looked at the other examples and have tried implementing them but without success. After starting the app action, my app plays a short sound and then waits a period of time, which it then plays another sound.
Things I have tried -
AVAudioPlayer not responding in the background
How to Play Audio in Background Swift?
Things I have learned so far is that I probably want to use the AVAudioSession with the Ambient category so that my sound mixes with the native music app. I have turned on the background capabilities in the tab and in the plist. I have tried putting the AVAudioSession in multiple places (delegate/VC file).
I am using the code from #2 to start my audioSession. Here is the code that plays the sound
func playComboSound(fileName:String){
if(fileName != "burpees" && fileName != "pushUps" && fileName != "sitUps")
{
let url = NSBundle.mainBundle().URLForResource(fileName, withExtension: "mp3")!
do {
appDelegate.audioPlayer = try AVAudioPlayer(contentsOfURL: url)
//guard let appDelegate.audioPlayer = audioPlayer else { return }
//appDelegate.audioPlayer!.stop()
appDelegate.audioPlayer!.prepareToPlay()
appDelegate.audioPlayer!.play()
} catch let error as NSError {
print(error.description)
}
}
else
{
let numRepAudio: String
switch numReps{
case "THREE":
numRepAudio = "three"
case "FIVE":
numRepAudio = "five"
case "TEN":
numRepAudio = "ten"
default:
numRepAudio = "three"
}
let url1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(numRepAudio, ofType: "mp3")!)
let url2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")!)
print(numRepAudio)
print(fileName)
let item1 = AVPlayerItem(URL: url1)
let item2 = AVPlayerItem(URL: url2)
let items: [AVPlayerItem] = [item1, item2]
appDelegate.playerQueue = AVQueuePlayer(items: items)
appDelegate.playerQueue!.play()
}
}
Thank you

Resources