Hey guys,
If you are like me and just now getting to do these courses, unfortunately, lots of times, functions that used to work in iOS8 and 9 are deprecated in iOS10+.
I personally using iOS11 and soon moving to iOS12.
This is for the Week 2, Module 6: Audio example, of how to play small sample audios.
I followed the video but my files won’t play, till I discovered that a new function is used now, which also contains a destructor, so you don’t need an extra dealloc funtion.
Because of the destructor, we’ll have to create the sound every time you push the button to play it.
This is the code I’ve used and works like a charm. Enjoy!
-
(IBAction)playSoundA:(id)sender {
// Play the sound A
// Archaic C code
// __bridge = C-level cast
// Tels ARC to stop taking notice of the casted object
// Casting -> Don’t generate ARC meta dataNSURL *urlA = [[NSBundle mainBundle] URLForResource:@“marimba.aif” withExtension:nil];
OSStatus statusReport = AudioServicesCreateSystemSoundID((__bridge CFURLRef)urlA, &_beepA);if(statusReport == kAudioServicesNoError) {
AudioServicesPlaySystemSoundWithCompletion(_beepA, ^{
AudioServicesDisposeSystemSoundID(self.beepA);
});} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Couldn't load beepA" message:@"BeepA problem" preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:alert animated:YES completion:nil];
}
}
-
(IBAction)playSoundB:(id)sender {
// Play the sound B
// Archaic C code
// __bridge = C-level cast
// Tels ARC to stop taking notice of the casted object
// Casting -> Don’t generate ARC meta data
NSURL *urlB = [[NSBundle mainBundle] URLForResource:@“sncf.aif” withExtension:nil];
OSStatus statusReport = AudioServicesCreateSystemSoundID((__bridge CFURLRef)urlB, &_beepB);if(statusReport == kAudioServicesNoError) {
AudioServicesPlaySystemSoundWithCompletion(_beepB, ^{
AudioServicesDisposeSystemSoundID(self.beepB);
});} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Couldn't load beepB" message:@"BeepB problem" preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:alert animated:YES completion:nil];
}
}
And you don’t have to put anything in viewDidLoad anymore.
take care,
Mircea