-1

i'm making a bubble popping game and hit a bit of a snag :/

i have a sound (pop.mp3), this sound needs to play EVERY time one of the bubbles are tapped

all the bubbles are buttons that call the same method (-(IBACTION)bubblePop)

i initialised an AVAudioPlayer in the Viewdidload method

and call it's play function inside the bubblePop method

this clearly did not work however because the bubble's pop sound only plays one at a time, it doest overlap like i would expect it to

does anybody know how i can resolve this?

Extra info

if i initialise the audio player inside bubblePop i get no sound at all

Community
  • 1
  • 1
Jamie McAllister
  • 729
  • 2
  • 11
  • 33

1 Answers1

0

You should initialize AVPlayer inside bubblePop and keep strong reference to it. Try this:

@interface ViewController()
@property (strong, nonatomic) NSMutableDictionary *players;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.players = [[NSMutableDictionary alloc] init];
}

- (void)bubblePop {
    NSURL *URL = your sound URL;

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:URL];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

    AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    self.players[playerItem] = player;

    [player play];
}


-(void)itemDidFinishPlaying:(AVPlayerItem *)sender {
    [self.players removeObjectForKey:sender];
}
@end
Roman Kabachenko
  • 513
  • 2
  • 10
  • thanks, i'll give that a go now, just to satisfy my noobbish-ness, strong and nonatomic, what do they mean? – Jamie McAllister Oct 26 '13 at 19:25
  • this doesnt work, all it does it make the sound restart. i need it to finish the sound that's playing and play the same sound again without waiting for the first sound to finish....... i'm not 100% sure that made sense :/ – Jamie McAllister Oct 26 '13 at 19:32
  • A strong reference (which you will use in most cases) means that you want to "own" the object you are referencing with this property/variable. The compiler will take care that any object that you assign to this property will not be destroyed as long as you (or any other object) points to it with a strong reference. Only once you set the property to nil will the object get destroyed (unless one or more other objects also hold a strong reference to it). – Roman Kabachenko Oct 26 '13 at 19:53
  • To prevent previous sound from stop, you can keep players in array. – Roman Kabachenko Oct 26 '13 at 19:53
  • again with my noobish-ness..... so do you mean create an array instead of the AVPlayer property and store new AVplayers in there? or a new array as well as the AVPlayer? – Jamie McAllister Oct 26 '13 at 19:57
  • First one. Create new NSMutableArray property and store new players. – Roman Kabachenko Oct 26 '13 at 20:06
  • i am assuming if i don't remove the after the sound finishes there could be potentially hundreds of players stored in the array by the end of the minute, that's probably a memory issue so how do i remove a player from the array after it's sound finishes? – Jamie McAllister Oct 26 '13 at 20:22