1

シンプルな効果音を再生するために、アプリで SystemSound を使用しました。これに加えて、MPMoviePlayerController を介してミュージックビデオを再生します。これで、音量を上げたり下げたりすると、ビデオの音楽が意図したとおりに応答します (音量を上げたり下げたりします)。

ただし、再生されるシステム サウンドは音量に反応しません。ユーザーがアプリの特定の領域をタップすると、システム音が鳴ります。そのための私のコードのスニペットを次に示します。

- (void)handleTap:(UITapGestureRecognizer *)recognizer {
   SystemSoundID completeSound = nil;

   //yellow folder in xcode doesnt need subdirectory param
   //blue folder (true folder) will need to use subdirectory:@"dirname"
   NSURL *sound_path  = [[NSBundle mainBundle] URLForResource: target_sound_filename withExtension: @"wav"];

   AudioServicesCreateSystemSoundID((__bridge CFURLRef)sound_path, &completeSound);
   AudioServicesPlaySystemSound(completeSound);
}

PS。「設定->サウンド->着信音とアラート->ボタンで変更」がオンに設定されていることを再確認しました(他のSOの回答を読んで、このオプションをオフのままにするとsystemsoundがボリュームボタンに応答しなくなります)

さらに、systemsound を使用する理由は、(ゲームのように) 複数のサウンドを再生するときに最も正確で応答性の高い結果が得られるためです。

可能であれば OpenAL を使用しないことをお勧めします ( FinchCocosDenshionなどのサードパーティのサウンド ライブラリを使用しても) 。

何か案は?

4

2 に答える 2

2

ユーザーの音量設定によって制御されるサウンド (システム サウンド以外) を再生するには、 AVAudioPlayerクラスを使用します。

AVAudioPlayer定期的に使用するサウンド ファイルごとに のインスタンスを保持し、単にplayメソッドを呼び出すことができます。prepareToPlayバッファをプリロードするために使用します。

于 2013-06-20T12:26:32.810 に答える
1

サウンド ファイルごとに AVAudioPlayer のインスタンスを保持し、prepareToPlay を使用してサウンドをプリロードできることを提案してくれた Marcus に乾杯します。同じ解決策を探している他の人を助けるかもしれないので、ここに私がそれをした方法があります(誰かが改善のための提案を持っている場合はコメントしてください)

//top of viewcontroller.m
@property (nonatomic, strong) NSMutableDictionary *audioPlayers;
@synthesize audioPlayers = _audioPlayers;

//on viewDidLoad
self.audioPlayers = [NSMutableDictionary new];

//creating the instances and adding them to the nsmutabledictonary in order to retain them
//soundFile is just a NSString containing the name of the wav file
NSString *soundFile = [[NSBundle mainBundle] pathForResource:s ofType:@"wav"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
//audioPlayer.numberOfLoops = -1;
[audioPlayer prepareToPlay];

//add to dictonary with filename (omit extension) as key
[self.audioPlayers setObject:audioPlayer forKey:s];

//then i use the following to play the sound later on (i have it on a tap event)
//get pointer reference to the correct AVAudioPlayer instance for this sound, and play it
AVAudioPlayer *foo = [self.audioPlayers objectForKey:target_sound_filename];
[foo play];

//also im not sure how ARC will treat the strong property, im setting it to nil in dealloc atm.
-(void)dealloc {
    self.audioPlayers = nil;
}
于 2013-06-24T23:31:31.163 に答える