代码:
NSString *soundName = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundName];
NSError *error = [[NSError alloc] init];
self.backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
if (self.backgroundPlayer == nil) {
NSLog(@"error = %@",[error description]);
} else {
[self.backgroundPlayer setDelegate:self];
[self.backgroundPlayer setNumberOfLoops:HUGE_VAL];
[self.backgroundPlayer setVolume:0.5f];
[self.backgroundPlayer prepareToPlay];
if ([self.backgroundPlayer prepareToPlay]) {
if ([self.backgroundPlayer play]) {
NSLog(@"playing");
}
} else {
NSLog(@"error!");
}
}
在将iphone调到静音模式时声音还会播放,不知道怎么让它停止呢?