Skip to content

Instantly share code, notes, and snippets.

@ykws
Created December 24, 2015 07:05
Show Gist options
  • Save ykws/b0f2fc9954f613f57b1e to your computer and use it in GitHub Desktop.
Save ykws/b0f2fc9954f613f57b1e to your computer and use it in GitHub Desktop.
Play background music on iOS
#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface MainViewController ()
@property (strong, nonatomic) AVAudioPlayer *player;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bgm" ofType:@"mp3"]] error:nil];
self.player.numberOfLoops = -1;
[self.player prepareToPlay];
[self.player play];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment