views:

18

answers:

1

I'm using an AVAudioPlayer to manage some sounds but the isPlaying method seems to be crashing.

done when I initiate the page:

self.soundClass = [AVAudioPlayer alloc];

how I play the sound:

-(void)playSound:(NSString *)fileName:(NSString *)fileExt {

    if ( [self.soundClass isPlaying] ){
        [self.soundClass pause];
    }
    else if (newSoundFile == currentSoundFile) {
        [self.soundClass play];
    }
    else {
        NSLog(@"PlaySound with two variable passed function");
        [[NSBundle mainBundle] pathForResource: fileName ofType:fileExt]], &systemSoundID);

        [self.soundClass initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: fileName ofType:fileExt]] error:nil];

        [self.soundClass prepareToPlay];
        [self.soundClass play];
    }

    self.currentSoundFile = fileName;

}

My soundClass is pretty empty right now:

soundclass.h

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import<AVFoundation/AVFoundation.h>


@interface SoundClass : AVAudioPlayer <AVAudioPlayerDelegate> {

}

@end

SoundClass.m

#import "SoundClass.h"

@implementation SoundClass 
-(void)dealloc {


    [super dealloc];
}

@end

Do you see anything here I might be doing wrong? It crashes right at if ( isPlaying )

+2  A: 

You point to space that is allocated for an instance of AVAudioPlayer in the first line you list, but you don't actually initialize an instance. In Cocoa, "alloc" is 99.9% of the time followed by some form of -init (like -initWithContentsOfURL:error: in the case of AVAudioPlayer).

Also, "soundClass" is an odd name for an instance variable. You should read up on the difference between a class (the blueprint of an object) and an instance of a class (an actual object built from the blueprint). Solid knowledge of this concept is critical to your understanding of Cocoa (and all object-oriented) programming.

Joshua Nozzi
Hmm, when I'm starting out and the program hasn't been told what to play yet can I init with nothing? Or should I just load something up? If I'm changing clips do I just re-initWithContentsOfURL?
emachine
In that case, set it to nil. This is usually done in your class's init... method (the class of the instance to which "self" points this case).
Joshua Nozzi
so self.soundClass = nil? Doesn't that set it up to be deleted?
emachine
"soundClass" is a property - a pointer to an instance variable.If an object is assigned to it and you set it to nil (and nothing else has an "interest" in it) then that object will go away, but your pointer remains (and is pointing to nil until you tell it otherwise).
Joshua Nozzi
Hmm, interesting. Didn't fix it though. I keep getting a EXC_BAD_ACCESS signal. Not really sure how I find out more about the error.
emachine
You're probably going to have to post more of your code. Your class's init method, your property and synthesize statements (doubt there's a problem there, though), etc.
Joshua Nozzi
I just initWithContentsOfURL'd it with a small sound and it works now. Would this be an acceptable solution or an unrecommended one?
emachine
Depends if you want a default sound when you initialize your object. Either way, you either nil it out or assign it to a valid object that will stick around (ie, alloc/init, retain, copy...).
Joshua Nozzi