Problems playing multiple sounds using AVPlayer (NOT AVAudioPlayer)

Posted by myetter37 on Stack Overflow See other posts from Stack Overflow or by myetter37
Published on 2012-06-04T04:26:25Z Indexed on 2012/06/04 4:40 UTC
Read the original article Hit count: 460

Filed under:
|
|

I'm trying to play a background song in my iPhone game and also have sound effects, using the AVFoundation framework and AVPlayerItem. I've scoured the Internet for help with AVPlayerItem and AVPlayer but I'm only finding stuff about AVAudioPlayer.

The background song plays fine, but when the character jumps, I have 2 problems:

1) On the initial jump ([player play] inside jump method), the jump sound effect interrupts the background music.

2) If I try to jump again, the game crashes with the error "AVPlayerItem cannot be associated with more than one instance of AVPlayer"

My professor told me to create a new instance of AVPlayer for each sound I want to play, so I'm confused.

I'm doing data driven design, so my sound files are listed in a .txt and then loaded into a NSDictionary.

Here's my code:

- (void) storeSoundNamed:(NSString *) soundName 
        withFileName:(NSString *) soundFileName
{
    NSURL *assetURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:soundName ofType:soundFileName]];

    AVURLAsset *mAsset = [[AVURLAsset alloc] initWithURL:assetURL options:nil];

    AVPlayerItem *mPlayerItem = [AVPlayerItem playerItemWithAsset:mAsset];

    [soundDictionary setObject:mPlayerItem forKey:soundName];

    NSLog(@"Sound added.");
}

- (void) playSound:(NSString *) soundName
{
    // from .h: @property AVPlayer *mPlayer;
    // from .m: @synthesize mPlayer = _mPlayer;       

    _mPlayer = [[AVPlayer alloc] initWithPlayerItem:[soundDictionary valueForKey:soundName]];

    [_mPlayer play];
    NSLog(@"Playing sound.");
}

If I move this line from the second method into the first:

_mPlayer = [[AVPlayer alloc] initWithPlayerItem:[soundDictionary valueForKey:soundName]];

The game does not crash, and the background song will play perfectly, but the jump sound effect does not play, even though the console is showing "Playing sound." on each jump.

Thank you

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about xcode4