Audio File continues to play even on leaving the view
What I am doing is
-(void)viewWillAppear:(BOOL)animated{
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(clickEvent:) userInfo:nil repeats:YES];
}
-(void)clickEvent:(NSTimer *)a开发者_Python百科Timer{
NSDate* finishDate = [NSDate date];
if([finishDate timeIntervalSinceDate: self.startDate] > 11 && touched == NO){
NSString *mp3Path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.mp3"];
[self playMusicFile:mp3Path];
NSLog(@"Timer from First Page");
[aTimer invalidate];
//[touchCheckTimer release];
aTimer = nil;
}
else{
}
-(void)playMusicFile:(NSString *)mp3Path{
NSURL *mp3Url = [NSURL fileURLWithPath:mp3Path];
NSError *err;
AVAudioPlayer *audPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mp3Url error:&err];
[self setAudioPlayer1:audPlayer];
if(audioPlayer1)
[audioPlayer1 play];
[audPlayer release];
}
Now, on pushing another view this audio file keeps playing in the background. Please help!
Well it is very common mistake
when you are leaving the view call stop method on the AVAudioPlayer variable pointer that you used to play the audio with
精彩评论