I have AVAudioPlayer instance playing music. The instance is in the controller for the main view on my main window - it isn't very fancy, but it works:
in the .h
@interface ViewController () {
AVAudioPlayer *music;
}
@end
in the .m
- (void)viewDidLoad
{
[super viewDidLoad];
music = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/music01.mp3", [[NSBundle mainBundle] resourcePath]]] error:nil];
[music playAtTime:0]; // play from start
[music prepareToPlay];
}
-(void)playMusic {
if (music) {
[music play];
}
}
So far so good. Except that if I add a view (programatically, with it's own NSViewController) to the main view the music stops. Suddenly. Is this normal? What have I done wrong?
My intuition is that you somehow call stop on your AVAudioPlayer
instance when that view gets added to the window. There are many ways in which you can debug this, but, I'd put a symbolic breakpoint (you can read more about this kind of breakpoints here on SO) in the stop
method and see if it gets called and from where. Also, check if you don't call viewDidLoad
again when you add a new view (due to the inheritance you might have in place)
Otherwise, there's no way adding a view might trigger a system issue that might stop your instance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.