3

我的问题有点模糊,这就是我发布所有代码的原因,所以我要求每个人在给我答案之前请测试所有这些代码。谢谢

在此处输入图像描述

在我的应用程序中,我以UIButtons编程方式创建所有内容,然后将所有这些内容保存UIButtonsNSMutableArray. 这是我的代码:-

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

现在在 (aMethod:) 中,我在每个UIButton TouchUpInside事件上添加点击声音。这是我的代码。

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

在我的应用程序启动时一切正常,但是UIButtons在 100 次点击后连续点击大约我没有听到点击的声音,当我点击 Next UIButtonstouchup inside event 它崩溃时。任何人都可以帮助我解决这个问题。Thanx

4

4 回答 4

2

应用程序由于未捕获的异常NSInternalInconsistencyException,原因:无法在包中加载 NIBNSBundle </Users/moon/Library/Application Support/iPhone Simulator/4.1/Applications/3E14E5D6-4D1B-4DAC-A2B9-07667E99C399/soundtesting.app‌​&gt; (loaded)名称为 secondclass

是啊。我们在这里遇到的是无法理解日志消息。你的UIButton,我只能假设将一个新视图推送到堆栈上,它正在引用一个根本不存在的 NIB。这是一个命名问题,而不是按钮问题。最有可能的是,我会检查是否有任何调用-initWithNibName:,看看你的 NIB 是否真的被命名为“二等”。请记住,iOS 文件名是 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];
}
于 2012-06-27T07:19:22.437 回答
1

原因可能是我多次播放相同音频的内存泄漏它可能会填满内存使用此代码播放音频

- (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];
}
于 2012-06-27T07:15:48.567 回答
1

我测试你的代码。aMethod 以下代码中的更改。虽然这我可以获得标签值。

- (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];   
}
于 2012-06-27T07:17:16.603 回答
1

你有内存问题。当你这样做:

- (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;
    [音频播放];
}

1/ 路径永远不会被释放
2/ 音频永远不会被释放

所以你需要在委托方法中释放 theAudio :

– audioPlayerDidFinishPlaying:成功:

你需要像这样发布路径:

- (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;
    [音频播放];
    [路径释放];
}

我认为你的问题应该得到解决。

于 2012-06-27T07:22:36.863 回答