我有背景音乐,在我的app委托中为我的所有viewcontrollers声明。
appdelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, AVAudioPlayerDelegate>{
AVAudioPlayer *backgroundMusic;
}
appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *backgroundMusicString =[[NSBundle mainBundle]pathForResource:@"backgroundmusic" ofType:@"mp3"];
backgroundMusic = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:backgroundMusicString] error:nil];
backgroundMusic.delegate=self;
backgroundMusic.numberOfLoops=-1;
backgroundMusic.volume=0.2;
[backgroundMusic play];
return YES;
}
工作正常。但是我如何在viewcontroller中暂停我的背景音乐? 我有一个按钮可以关闭它,但是[backgroundMusic pause];不起作用。
- (IBAction)backgroundMusicOnOff:(id)sender {
}