如何在改变视图时停止背景音乐?我没有任何线索。如果我按下一个按钮,将我带到一个新的视图,就会有新的背景音乐。但旧的背景音乐(无限循环)仍在继续。请帮忙!还请示例一些代码,这是我的:
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"MathMusic2" ofType:@"wav"];
AVAudioPlayer* theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
theAudio.numberOfLoops = -1;
[super viewDidLoad];
}
我只需要知道如何使新视图中的背景音乐停止播放。当我从新视图按下后退按钮时反之亦然
为AVAudioPlayer *theAudio
这样您就可以从班级中的任何位置访问audioPlayer。
viewController的头文件
...
AVAudioPlayer *theAudio;
...
@property (nonatomic, retain) AVAudioPlayer *theAudio;
viewController的实现文件
...
@synthesize theAudio;
...
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"MathMusic2" ofType:@"wav"];
self.theAudio= [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]] autorelease];
theAudio.delegate = self;
[theAudio play];
theAudio.numberOfLoops = -1;
[super viewDidLoad];
}
如果 viewWillDisappear 被调用,您可以使用以下命令停止音频
- (void)viewWillDisappear
{
[theAudio stop];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)