iOS - 如何在更改视图时停止背景音乐
问题描述:
如何在更改视图时停止背景音乐?我没有任何线索。如果我按下一个按钮,带我到一个新的视图,有新的背景音乐。但是旧的背景音乐(无限循环)一直在继续。请帮忙!也请示例一些代码,这里是我的:iOS - 如何在更改视图时停止背景音乐
- (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的Implentation文件
...
@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];
}
答
它在这里给出了错误: “theAudio.delegate = self;”就像这样分配给ID ...
它的黄色反正.. 仍然当我改变看法,并转到另一级的音乐不回采....
+1
在viewController.h中添加AVAudioPlayerDelegate – Nishant 2012-12-28 19:14:04
太多errorrs – user722566 2011-04-23 15:14:24
@ user721820赦免? – 2011-04-23 15:15:11