Reputation: 5896
I'm having this unstable behavior with AudioServicesPlaySystemSound
.
For some reason, over my (real devices) iPhone 5/6 everything works perfectly.But sometimes over my (real device) iPhone 6+S, i'm hearing -Sometimes- only 3 or 2 sounds out of 5 that i'm playing
The function i used to create this sound :
if let soundURL = NSBundle.mainBundle().URLForResource(soundName, withExtension: "mp3") {
var mySound: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundURL, &mySound)
AudioServicesPlaySystemSound(mySound);
}
Upvotes: 2
Views: 734
Reputation: 6505
you should do some debug in the code and find out the real cause of the problem, the following code will print out the OSStatus:
let result = AudioServicesCreateSystemSoundID(soundURL, &mySound)
print(result)
you need to figure out what's wrong when the result is not 0, the most common issue is that the sound file should less than 30 sec., otherwise it will not be able play by system sound service.
another issue is you should create system sound in the start of your app, then play that returned mySound when needed, do not call AudioServicesCreateSystemSoundID every time when you play it.
import UIKit
import AudioToolbox
class ViewController: UIViewController {
var mySound: SystemSoundID = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let soundName = "mytest"
if let soundURL = NSBundle.mainBundle().URLForResource(soundName, withExtension: "mp3") {
let result = AudioServicesCreateSystemSoundID(soundURL, &mySound)
print(mySound)
print(result)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func play (sender: UIButton) {
AudioServicesPlaySystemSound(mySound);
}
}
Upvotes: 2