3

My question is little abmigious ,thats why I posted all my code,so I request to every one please test all this code before giving me the Answer.Thanx

enter image description here

In my application i create all UIButtons programmatically and then save all these UIButtons in NSMutableArray. Here is my code:-

-(void)button:(id)sender
{
int btnn = 0;
int spacex = 152;
int spacey=20;
int k=0;
saveBtn = [[NSMutableArray alloc] init];
for (int i=0; i<48; i++)
      {
          if (btnn>6)
          {
              spacey=spacey+25;
              spacex = 152;
              btnn = 0;
          }
          else
          {
              btnn++ ;
              k++;
              UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
              btn.frame = CGRectMake(spacex, spacey, 25.0, 25.0);
              int idx;
              idx = arc4random()%[arr count];
              NSString* titre1 = [arr objectAtIndex:idx];
              [btn setTitle: titre1 forState: UIControlStateNormal];
              [btn setTitleColor: [UIColor yellowColor] forState: UIControlStateNormal];
              [btn.titleLabel setFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:22.0]];
              spacex = spacex + 25;
              btn.tag=k;
              [btn addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
              [saveBtn addObject:btn];
              [self.view addSubview:btn];
        }
    }
}   

Now in (aMethod:) I add click sound on Each UIButton TouchUpInside event. Here is my code.

- (void)aMethod:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate=self;
    [theAudio play];
}

Every thing work fine in start of my application , but continiously clicking the UIButtons approximatilly after 100 clicks i hear no sound of click and when i click on Next UIButtons touchup inside event its crash.Can any body help me in this issue.Thanx

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
jamil
  • 2,419
  • 3
  • 37
  • 64
  • 2
    Anything in the log when it crashes? And this question isn't as weird as you think it is ;) – CodaFi Jun 27 '12 at 06:59
  • 1
    Crash log will help in identifying the issue, and also you need to enable NSZombie, in these kind of cases, most of time memory is reason for crash. – rishi Jun 27 '12 at 07:01
  • Thanx for reply after my UIButtons gives me no sound when i click on Next UIButtons in give me in NSLog.. Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'secondclass'' – jamil Jun 27 '12 at 07:15

4 Answers4

2

app due to uncaught exception NSInternalInconsistencyException, reason: Could not load NIB in bundle: NSBundle </Users/moon/Library/Application Support/iPhone Simulator/4.1/Applications/3E14E5D6-4D1B-4DAC-A2B9-07667E99C399/soundtesting.app‌​> (loaded) with name secondclass

Ah, yes. What we have here is a failure to understand log messages. Your UIButton, which I can only assume pushes a new view onto the stack, is referencing a NIB that simply doesn't exist. It's a naming issue, not a button issue. Most likely, I would check for any calls to -initWithNibName: and see if your NIB really is named "secondclass.". Remember, iOS file names are CaSe SeNsITive.

//.h
...
@property (nonatomic, retain) AVAudioPlayer * player;

//.m
-(void)viewDidLoad
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
    player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    player.delegate=self;
    //in MRC, use [path release];
}

-(void)someMethod:(id)sender
{
    [player play];
}
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • Thanx but the same code work fine for me..before before my buttons get silent..or give me no sound. – jamil Jun 27 '12 at 07:22
  • 1
    Yes, that... I believe it could be that you are alloc'ing 100 instances of AVAudioPlayer. Why not just have one property? It's a memory thing. – CodaFi Jun 27 '12 at 07:24
  • 1
    Make your audioPlayer a property, then it's just a matter of calling `-play` – CodaFi Jun 27 '12 at 07:27
  • @Albert, I've edited in an example. Next time, _please_ don't alloc 100 instances of a class you'll reuse!!! – CodaFi Jun 27 '12 at 07:32
1

The reason might me memory leak for playing same audio these many times it may fill memory use this code for playing audio

- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundleOrNil
{
    if (self = [super initWithNibName:nibName bundle:nibBundleOrNil]) {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
        theAudio = [AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:NULL];
    }
    return self;
}

- (IBAction)playSound
{
   [theAudio play];
}

- (void)dealloc
{
   [theAudio release];
   [super dealloc];
}
Sumanth
  • 4,913
  • 1
  • 24
  • 39
1

I test your code . Changes in your aMethod following code. Though this I can get tag value.

- (void)aMethod:(id)sender
{
    UIButton *btn=(UIButton *)sender;

    NSLog(@"\n\n\nselectedbutton tag === %d \n\n\n",btn.tag );

    NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];

    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
    audioPlayer.delegate = self;

    [audioPlayer prepareToPlay];
    [audioPlayer play];   
}
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Senthilkumar
  • 2,471
  • 4
  • 30
  • 50
1

You have a memory issue. When you do :

- (void)aMethod:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate=self;
    [theAudio play];
}

1/ path is never released
2/ theAudio is never release

So you need to release theAudio in delegate method :

– audioPlayerDidFinishPlaying:successfully:

And you need to release path like this :

- (void)aMethod:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"button-17" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate=self;
    [theAudio play];
    [path release];
}

I think your problem should be solved.

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
TheRonin
  • 1,305
  • 1
  • 12
  • 18