Sound not working in iPhone Simulator?
On your Mac, go to System Preferences > Sound > Sound Effects and then uncheck and recheck "Play user interface sound effects".
You need to re-activate your system sounds, see the end of this page.
iOS Simulator does not play sound
The error occurs because you are trying to get device's sample rate
from simulator, which is not possible.
Because simulator is not a hardware. Its a software.
So some hardware functionality will not able to simulated with it.
It can not do the following:
- Open Camera
- Play sounds
- Gyrometer
- Accelerometer
- Shake effect
- Receive push notifications
And many more which required device hardware acceleration.
You need to test this in real device.
System Sound plays in iPhone simulator but on iphone not playing
Here's a small detail that many developers have wasted hours upon:
Case Sensitivity
Really, it all boils down to:
- iPhone Simulator: Case-Insensitive
- iOS device: Case-Sensitive
So, you might have 'waterfall' in your code, but the actual sound name is 'Waterfall'. It might even be something like 'wAtErFaLl'.
This will work fine on the simulator, but not on the device - the device simply won't find the sound.
This has happened to me for everything from sounds to images to plists.
Check your Cases!
Also, AudioServices has some guidelines on what can play in the device and what can't:
Sound files that you play using this function must be: - No longer than 30 seconds in duration - In linear PCM or IMA4 (IMA/ADPCM) format - Packaged in a .caf, .aif, or .wav file
sound is playing in simulator but not on physical device
Double check the code you've posted is not falling into the catch block. You should see an error in the console if an error is produced.
do {
print("ran")
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch let error {
print(error.localizedDescription)
}
You should also check the device is not on silent. To prevent this you can use the below that will always the play the audio as the audio sessions category will be set to .playback
do {
try AVAudioSession.sharedInstance().setCategory(.playback)
} catch let error {
print(error.localizedDescription)
}
Can't hear System Sound in simulator or on device
Don't dispose the sound until after it finishes playing. You can do something like this:
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, (AudioServicesSystemSoundCompletionProc)AudioServicesDisposeSystemSoundID, NULL);
Slightly icky, since we're assuming that it doesn't use the Pascal calling convention (i.e. AudioServicesDisposeSystemSoundID() can handle the extra NULL argument).
Related Topics
Restkit, Coredata and Swift - I Can't Seem to Fetch Results Back Out
Pass Data from View Controller to Child Controller in Swift
Why Sending Message from Watchkit Extension to iOS and Getting Back a Reply Is So Slow
Swift Scenekit - Centering Scntext - the Getboundingboxmin:Max Issue
Calculate Distance Between My Location and a Mapkit Pin on Swift
Sectioning Tableview and Rows with Core Data Swift
Circle Image in Cell Imageview and How to Resize It
How to Create a Rounded Rectangle Label in Xcode 7 and Swift 2
Cannot Set Color of Button's Label Inside Menu in Swiftui
Xcode 8 Memory Graph Says "No Selection" and Not Working
iOS Keyboard Active But Invisible When Uisearchbar Is Tapped
How to Get the User "Name" Using Swift
How to Add .Plist File to All Targets in Xcode
How to Bridge Nsnumber to Float in JSON Parsing
Code Coverage Result Is Not Accurate to Real Coverage in Xcode 7