2

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];ません。これでストーリーボードを使用しています。

4

2 に答える 2

0

現在のクラスに stop メソッドを追加し、ボタンのクリック時に他のクラスでそのメソッドを呼び出すことができます。次のリンクでコード スニペットを見つけてください。

AVAudioPlayer をトリガーして、再生 (選択) ボタンがクリックされた場合を除いて、すべてのサウンドを停止し、選択したものを再生します。

もう 1 つリンクを追加するビューを切り替えるときに、AVAudioPlayer の stop メソッドが機能しないのはなぜですか

于 2013-10-23T10:51:15.970 に答える
0

AVAudioPlayeratのインスタンスを.h fileグローバルとして宣言します。

@property (nonatomic, retain) AVAudioPlayer *theAudio;

これは別のコントローラーから停止できます

[objViewController.theAudio stop];

これは私にとってはうまくいきます..あなたにも役立つことを願っています..

于 2013-10-23T10:57:27.660 に答える