Skip to content

Instantly share code, notes, and snippets.

@alfonsodev
Created January 15, 2016 12:36
Show Gist options
  • Select an option

  • Save alfonsodev/dc2b644e968ab927fd37 to your computer and use it in GitHub Desktop.

Select an option

Save alfonsodev/dc2b644e968ab927fd37 to your computer and use it in GitHub Desktop.
Play a sound file in ObjC
// from http://codewithchris.com/avaudioplayer-tutorial/
@interface ViewController ()
{
AVAudioPlayer *_audioPlayer;
}
@end
// Construct URL to sound file
- (void)viewDidLoad
{
[super viewDidLoad];
// Construct URL to sound file
NSString *path = [NSString stringWithFormat:@"%@/drum01.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
// Create audio player object and initialize with URL to sound
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment