-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Homework Done - Diana #28
base: master
Are you sure you want to change the base?
Conversation
NSString *path = [NSString stringWithFormat:@"%@/Doorbell.mp3", [[NSBundle mainBundle] resourcePath]]; | ||
NSURL *soundUrl = [NSURL fileURLWithPath:path]; | ||
// Create audio player object and initialize with URL to sound | ||
_audioPlayerDoorbell = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI you can use NSBundle -URLForResource:withExtension:
(docs) to do this in fewer steps and without an explicit dependency on file paths:
NSURL *soundUrl2 = [[NSBundle mainBundle] URLForResource:@"PhoneRinging" withExtension:@".mp3"];
_audioPlayerPhoneRinging = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl2 error:nil];
This way, you can clean up your audio files into a subdirectory (group) within your bundle, like /Sounds
, so that they don't clutter up your project list:
@Diana71 - great work on this project. You definitely went above and beyond. 👍 💃 Things you did well:
I don't have that many suggestions for improvement, which is why I didn't add too many inline comments. However, there is one area where your code could be improved, because it breaks proper encapsulation principles. When you store all of the references to audio files w/in This is not great because it means that your view controller now needs to know a lot about the details of the model, and you have lots of logic there which relies on this dependency. To improve this, consider that the audio file is a form of data (i.e. state), and thus belongs to the model. It is still the controller's responsibility to actually play the audio file, but the model is responsible for allowing access to the audio file, or even exposing its own Here's one way you could restructure your code to separate concerns more appropriately between your controller and model:
Here's an example implementation of the #import "CQCategory.h"
@interface CQCategory ()
{
AVAudioPlayer *_audioPlayer;
}
@end
@implementation CQCategory
- (void)playSoundForSelection {
NSString *audioFileName = [self.selection stringByReplacingOccurrencesOfString:@" " withString:@""];
NSURL *audioFileURL = [[NSBundle mainBundle] URLForResource:audioFileName withExtension:@".mp3"];
if (audioFileURL == nil) {
return; // If there isn't an audio file to be found, don't try to play it
}
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
[_audioPlayer play];
}
@end That way, all you need to do in The last piece that needs to be fixed is the sound you play when segueing to the I'd suggest adding a new method to your |
No description provided.