AVAudioPlayer を使用してアプリでサウンドを再生します。その中でメソッドを呼び出すだけで音を止めることができ[audioPlayer stop];
ます。しかし、別のViewControllerからメソッドを呼び出すと、音が止まりません。すでに検索しましたが、正しい答えが得られません。私が欲しいのは、別のViewControllerからの音を止めることです。誰かが私を助けることができますか?私はiOSが初めてです。
これを使用してサウンドファイルを追加しました:
//Declare the audio file location and settup the player
NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:@"alarm" withExtension:@"wav"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
[audioPlayer setNumberOfLoops:-1];
if (error) {
NSLog(@"%@", [error localizedDescription]);
[[self volumeControl] setEnabled:NO];
[[self playPauseButton] setEnabled:NO];
[[self alertLabel] setText:@"Unable to load file"];
[[self alertLabel] setHidden:NO];
} else {
[[self alertLabel] setText:[NSString stringWithFormat:@"%@ has loaded", @"alarm.wav"]];
[[self alertLabel] setHidden:NO];
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
//Load the audio into memory
[audioPlayer prepareToPlay];}
そして [audioPlayer play];
、音を出したり止めたりするのに使ってい[audioPlayer stop];
ました。同じViewControllerを使用しているときは正常に動作していますが、ViewControllerを変更すると動作し[myViewController.audioPlayer stop];
ません。これでストーリーボードを使用しています。