Playing Multiple Sounds at Once Using AVAudioPlayer

旧街凉风 提交于 2019-12-11 05:15:03

问题


I have a set of UIButtons. Every time I press a button, a sound plays. The issue: When you press one button and the sound behind to play, when you press the second button the sound coming from the first button stops.

This question has been asked a million times. The solution is always to create a new instance of AVAudioPlayer each time. I've tried that, to no avail. I've read the docs, read through every question on StackOverflow related to this, and have had no luck. When I use SystemSoundID they play simultaneously each time... But I have to sacrifice volume control ability. Here is the code I'm working with:

 NSString *soundOne = [[NSUserDefaults standardUserDefaults] valueForKey:senderString];

    NSURL *url = [NSURL URLWithString:[soundOne stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    switch ([sender tag]) {

        case 1:
            [_audioPlayer prepareToPlay];
            [_audioPlayer play];
            NSLog(@"Playing: %@", soundOne);
            break;

        case 2:
            [_audioPlayer prepareToPlay];
            [_audioPlayer play];
            NSLog(@"Playing: %@", soundOne);
            break;
}

回答1:


The problem is that your code, while making a new audio player, also destroys the old audio player. You have to have two audio players existing at the same time in order to get them to play two sounds at the same time. That can't possibly happen when you have only one instance variable _audioPlayer.




回答2:


Taken from matt's answer, playing the second sound destroys the first.

Consider this:

x = 3

We just set x to equal 3. Now, we need to store another number, 4. Now, we have this:

x = 4

Wait, we just assigned x to be 4. We forgot 3. We need a second variable to hold 3. Why not y?

y = 3

There we go. Now we have x = 4 and y = 3. Two variables, holding two values.

This goes along with instances of audio players. Whether the button is the first or second, the audio player is being reset to be an instance of whatever sound you want to play. So, what happens is, the audio player forgets the first sound and plays the second. It's like resetting x to be 4, not 3.

Just like the solution above, you need a second variable.


Replace this in your .h file...

@property AVAudioSession* audioPlayer;

with...

@property AVAudioSession* audioPlayer1;
@property AVAudioSession* audioPlayer2;

May I notice also, in your code, you always re-initialize the audio player every time a sound plays, and you only have one sound set to play. In your .m file, copy/paste this function, or add the relevant code into your init function.

-(instancetype)init {
    self = [super init];
    if (self) {
        self->_audioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[[NSUserDefaults standardUserDefaults] valueForKey:Sound1] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
        self->_audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[[[NSUserDefaults standardUserDefaults] valueForKey:Sound2] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
        [self->_audioPlayer1 prepareToPlay];
        [self->_audioPlayer2 prepareToPlay];
    }
}

And, in your .h file, add:

#define Sound1 @"your first key name"
#define Sound2 @"your second key name"

Finally, you can switch the code in your function to just this:

NSLog(@"Playing sound with sender tag: %i", [sender tag]);
switch ([sender tag]) {
    case 1:
    {
        [_audioPlayer1 play];
        break;
    }
    case 2:
    {
        [_audioPlayer2 play];
        break;
    }
    default:
    {
        break;
    }
}


来源:https://stackoverflow.com/questions/27681276/playing-multiple-sounds-at-once-using-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!