1

UISwitch を介して有効になっているストップウォッチ アラームを設定しようとしています。タイマーが開始してからx秒後にアラームをトリガーしたい。経過した秒数を保持する ivar int 変数 (timerCount) があり、その ivar に基づいてサウンドを再生したいと考えています。タイマーが開始し、スイッチを有効にすると、プレーヤーが 10 秒で発砲することを期待しますが、そうではありません。「ifステートメント」が原因であることはわかっています。これを削除すると、スイッチが有効になっているときにAVPlayerがwavファイルを再生するからです。これを見て、私が見逃したものを理解するために、別の目玉が必要です。コード スニペットは次のとおりです。

- (IBAction)timerSound{

    if (voiceSwitch.on){

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"horn"
                                                         ofType:@"wav"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];

    if (timerCount == 10)

    [self.player play];

    }

    else {
        [self.player stop];
    }
}
4

1 に答える 1

1

を探していplayAt:ます。

あなたの例に基づく例 –</p>

- (IBAction)valueChanged:(id)sender {
    UISwitch *aSwitch = (UISwitch*)sender;
    if ( aSwitch.on ) {
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"horn"
                                                                  ofType:@"wav"];

        NSURL *fileURL = [NSURL fileURLWithPath:soundFilePath];

        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL 
                                                                error:nil];

        [audioPlayer playAtTime:(audioPlayer.deviceCurrentTime + 3)];
    } else {
        [audioPlayer stop];
    }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [self.theSwitch setOn:NO animated:YES];

    [player release];
}
于 2011-05-23T18:11:59.543 に答える