Nguyen Anh Tuan
Nguyen Anh Tuan

Reputation: 23

Playing music in background ios app

In my code:

- (void) viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"www/media/BlueZedEx" ofType:@"mp3"]];

AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];

NSLog(@"goldenace launchOptions = %@", musicFile);
click.delegate = self;
click.numberOfLoops = -1;
[click play];

}

But nothing music play when I run my app in simulator. Please help!

Upvotes: 1

Views: 2892

Answers (2)

Ashini
Ashini

Reputation: 492

use this for Playing Background Audio:

 [[AVAudioSession sharedInstance] setDelegate: self];
 NSError *setCategoryError = nil;
 [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

refer this link

Upvotes: 1

GeneCode
GeneCode

Reputation: 7588

This

www/media/BlueZedEx

is not a filename. It needs to be a filename of a audio file inside your project (drag and drop)

Upvotes: 0

Related Questions