0

私は Objective-C と他の環境の両方でまったくの初心者なので、しばらくお待ちください。これが私の問題です。サウンドファイルを再生して、他のファイルが開始されたときにそれぞれが停止するようにする必要があります。現在、それらは重複しています。ここに私のコードのスニペットがあります。事前に感謝し、他の詳細が必要な場合はお知らせください.

- (IBAction)soundfirst {
    NSString *path =
        [[NSBundle mainBundle] pathForResource:@"hi2" ofType:@"mp3"];

    player1 = [[AVAudioPlayer alloc]
        initWithContentsOfURL:[NSURL fileURLWithPath:path]
                        error:NULL];

    player1.delegate = self;
    [player1 play];
}
- (IBAction)soundsecond {
    [player1 stop];

    NSString *path =
        [[NSBundle mainBundle] pathForResource:@"hi2" ofType:@"mp3"];

    player2 = [[AVAudioPlayer alloc]
        initWithContentsOfURL:[NSURL fileURLWithPath:path]
                        error:NULL];

    player2.delegate = self;
    [player2 play];
}
4

2 に答える 2

0
#import <AudioToolbox/AudioServices.h>

SystemSoundID buttonClickSoundID;

+ (void)playButtonClickSound {
    if (buttonClickSoundID == 0) {
        NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"button_click" ofType:@"wav"];
        CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:soundPath];
        AudioServicesCreateSystemSoundID(soundURL, &buttonClickSoundID);
    }
    AudioServicesPlaySystemSound(buttonClickSoundID);
}
于 2013-07-04T11:56:23.187 に答える