我有一个应用程序可以跨不同的 ViewController 播放和控制音乐。为此,我在应用程序委托的 DidFinishLaunchingMethod 中创建了两个 AVAudioPLayer 实例:
NSString *path = [[NSBundle mainBundle] pathForResource:@"minnie1" ofType:@"mp3"];
NSURL *path1 = [[NSURL alloc] initFileURLWithPath:path];
menuLoop =[[AVAudioPlayer alloc] initWithContentsOfURL:path1 error:NULL];
[menuLoop setDelegate:self];
menuLoop.numberOfLoops = -1;
[menuLoop play];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"minnie2" ofType:@"mp3"];
NSURL *path3 = [[NSURL alloc] initFileURLWithPath:path2];
gameLoop=[[AVAudioPlayer alloc] initWithContentsOfURL:path3 error:NULL];
gameLoop.numberOfLoops = -1;
[gameLoop setDelegate:self];
[gameLoop prepareToPlay];
在此之后,我在各种视图控制器中调用它,以使用如下代码停止或重新启动:
- (IBAction)playGameLoop{
NSLog(@"Begin playGameLoop");
FPAppDelegate *app = (FPAppDelegate *)[[UIApplication sharedApplication] delegate];
if ([FPAVManager audioEnabled] == NO){
//DO NOTHING
}
else {
if (app.gameLoop.playing ==YES ) {
//DO NOTHING
}
else { [app.gameLoop play];
NSLog(@"End playGameLoop");
}
}
音频文件第一次播放良好,并在被要求停止时停止。但是,在 iOS4 设备上,它们不会在再次调用时开始重播。
谢谢!