さまざまなViewControllerで音楽を再生および制御するアプリがあります。これを行うために、アプリ デリゲートの DidFinishLaunchingMethod で AVAudioPLayer の 2 つのインスタンスを作成しました。
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];
この後、次のようなコードを使用して停止または再起動するために、さまざまなviewControllerで呼び出します。
- (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 デバイスでは、再度呼び出されたときに再生を開始しません。
ありがとう!