質問

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

役に立ちましたか?

解決

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];
}

他のヒント

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];
}

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.

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];   
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top