4

我是新手,我可能做错了很多事情,因此,我真的需要您的专业知识和帮助!

我正在创建一个使用 AVFoundation 框架的音乐应用程序。当我在我的 Macbook 和 iOS 设备上测试它时,它实际上在一开始就运行良好。但是在我按50-100次不同的音频播放按钮后,它会随机停止工作。该应用程序不会崩溃,但它不会播放任何声音。我真的不知道为什么,请帮助我。

更新(2013 年 6 月 7 日):在这里,现在我知道问题出在哪里,但我不明白......它在调试器中这样说:shm_open failed: "AppleAudioQueue.39.189049" (25) flags=0x2 errno =24 如果可能,请提供帮助:o!

这是我的代码:

[array1 retain];[array1 retain];[array1 retain];
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:[NSString stringWithFormat:@"%@", [[Scale objectAtIndex:array1.retainCount]description]] ofType:@"mp3" inDirectory:@"Piano"];

NSURL *url = [NSURL fileURLWithPath: path];
player = [[AVAudioPlayer alloc]
          initWithContentsOfURL:url error:nil];
[player setNumberOfLoops:0];
[player play];

这个问题的一个潜在原因是因为我正在使用[array retain/release]和使用数组retainCount来引用许多不同的对象。这会导致内存泄漏或类似情况吗?

你好bbum,谢谢你的帮助。供您参考,这是我愚蠢的代码的一部分。我知道这有点乱,如果您需要我的解释或更多详细信息,请告诉我:

    - (IBAction)P1C:(id)sender{


KeyCountA1 = [NSMutableArray arrayWithObjects:@"1",@"3",@"5",@"8",@"10",@"12",@"15",@"17",@"19",@"22",@"24",@"26",@"29",@"31",@"33",@"36",@"38",@"40",@"43",@"45",@"47",@"50",@"52",nil];
KeyCountA2 = [NSMutableArray arrayWithObjects:@"0",@"2",@"4",@"6",@"9",@"11",@"13",@"16",@"18",@"20",@"23",@"25",@"27",@"30",@"32",@"34",@"37",@"39",@"41",@"44",@"46",@"48",@"51",nil];
KeyCountA3 = [NSMutableArray arrayWithObjects:@"0",@"3",@"5",@"7",@"10",@"12",@"14",@"17",@"19",@"21",@"24",@"26",@"28",@"31",@"33",@"35",@"38",@"40",@"42",@"45",@"47",@"49",@"52",nil];
KeyCountA4 = [NSMutableArray arrayWithObjects:@"1",@"4",@"6",@"8",@"11",@"13",@"15",@"18",@"20",@"22",@"25",@"27",@"29",@"32",@"34",@"36",@"39",@"41",@"43",@"46",@"48",@"50",nil];
KeyCountA5 = [NSMutableArray arrayWithObjects:@"2",@"5",@"7",@"9",@"12",@"14",@"16",@"19",@"21",@"23",@"26",@"28",@"30",@"33",@"35",@"37",@"40",@"42",@"44",@"47",@"49",@"51",nil];
KeyCountA6 = [NSMutableArray arrayWithObjects:@"1",@"3",@"6",@"8",@"10",@"13",@"15",@"17",@"20",@"22",@"24",@"27",@"29",@"31",@"34",@"36",@"38",@"41",@"43",@"45",@"48",@"50",@"52",nil];
KeyCountA7 = [NSMutableArray arrayWithObjects:@"2",@"4",@"7",@"9",@"11",@"14",@"16",@"18",@"21",@"23",@"25",@"28",@"30",@"32",@"35",@"37",@"39",@"42",@"44",@"46",@"49",@"51",nil];

KeyCountC1 = [NSMutableArray arrayWithObjects:@"1",@"8",@"15",@"22",@"29",@"36",@"43",@"50",nil];
KeyCountC2 = [NSMutableArray arrayWithObjects:@"2",@"9",@"16",@"23",@"30",@"37",@"44",@"51",nil];
KeyCountC3 = [NSMutableArray arrayWithObjects:@"3",@"10",@"17",@"24",@"31",@"38",@"45",@"52",nil];
KeyCountC4 = [NSMutableArray arrayWithObjects:@"4",@"11",@"18",@"25",@"32",@"39",@"46",nil];
KeyCountC5 = [NSMutableArray arrayWithObjects:@"5",@"12",@"19",@"26",@"33",@"40",@"47",nil];
KeyCountC6 = [NSMutableArray arrayWithObjects:@"6",@"13",@"20",@"27",@"34",@"41",@"48",nil];
KeyCountC7 = [NSMutableArray arrayWithObjects:@"7",@"14",@"21",@"28",@"35",@"42",@"49",nil];

   //(16-May-2013) Refering to SA&CC and use the right scles play different chords for progression (refer to
//(22-May-2013) adding scales (13-120)

    if ((((ScaleArray.retainCount==1)||(ScaleArray.retainCount==2)||(ScaleArray.retainCount==14)||(ScaleArray.retainCount==25)||(ScaleArray.retainCount==26)||(ScaleArray.retainCount==38)||(ScaleArray.retainCount==49)||(ScaleArray.retainCount==50)||(ScaleArray.retainCount==62)||(ScaleArray.retainCount==73)||(ScaleArray.retainCount==74)||(ScaleArray.retainCount==85)||(ScaleArray.retainCount==86)||(ScaleArray.retainCount==98)||(ScaleArray.retainCount==110)||(ScaleArray.retainCount==111))&&(ChordCount.retainCount==1))||
        (((ScaleArray.retainCount==3)||(ScaleArray.retainCount==4)||(ScaleArray.retainCount==15)||(ScaleArray.retainCount==16)||(ScaleArray.retainCount==27)||(ScaleArray.retainCount==28)||(ScaleArray.retainCount==39)||(ScaleArray.retainCount==40)||(ScaleArray.retainCount==51)||(ScaleArray.retainCount==52)||(ScaleArray.retainCount==63)||(ScaleArray.retainCount==64)||(ScaleArray.retainCount==75)||(ScaleArray.retainCount==76)||(ScaleArray.retainCount==87)||(ScaleArray.retainCount==88)||(ScaleArray.retainCount==99)||(ScaleArray.retainCount==100)||(ScaleArray.retainCount==112))&&(ChordCount.retainCount==7))||
        (((ScaleArray.retainCount==5)||(ScaleArray.retainCount==17)||(ScaleArray.retainCount==18)||(ScaleArray.retainCount==29)||(ScaleArray.retainCount==30)||(ScaleArray.retainCount==41)||(ScaleArray.retainCount==42)||(ScaleArray.retainCount==53)||(ScaleArray.retainCount==54)||(ScaleArray.retainCount==65)||(ScaleArray.retainCount==66)||(ScaleArray.retainCount==77)||(ScaleArray.retainCount==89)||(ScaleArray.retainCount==101)||(ScaleArray.retainCount==102)||(ScaleArray.retainCount==113)||(ScaleArray.retainCount==114))&&(ChordCount.retainCount==6))||
        (((ScaleArray.retainCount==6)||(ScaleArray.retainCount==7)||(ScaleArray.retainCount==19)||(ScaleArray.retainCount==31)||(ScaleArray.retainCount==43)||(ScaleArray.retainCount==55)||(ScaleArray.retainCount==67)||(ScaleArray.retainCount==68)||(ScaleArray.retainCount==78)||(ScaleArray.retainCount==79)||(ScaleArray.retainCount==90)||(ScaleArray.retainCount==91)||(ScaleArray.retainCount==103)||(ScaleArray.retainCount==115)||(ScaleArray.retainCount==116))&&(ChordCount.retainCount==5))||
        (((ScaleArray.retainCount==8)||(ScaleArray.retainCount==9)||
          (ScaleArray.retainCount==20)||(ScaleArray.retainCount==21)||(ScaleArray.retainCount==32)||(ScaleArray.retainCount==33)||(ScaleArray.retainCount==44)||(ScaleArray.retainCount==45)||(ScaleArray.retainCount==56)||(ScaleArray.retainCount==57)||(ScaleArray.retainCount==69)||(ScaleArray.retainCount==80)||(ScaleArray.retainCount==81)||(ScaleArray.retainCount==92)||(ScaleArray.retainCount==93)||(ScaleArray.retainCount==104)||(ScaleArray.retainCount==105)||(ScaleArray.retainCount==117))&&(ChordCount.retainCount==4))||
        (((ScaleArray.retainCount==10)||(ScaleArray.retainCount==22)||(ScaleArray.retainCount==34)||(ScaleArray.retainCount==46)||(ScaleArray.retainCount==47)||(ScaleArray.retainCount==58)||(ScaleArray.retainCount==59)||(ScaleArray.retainCount==70)||(ScaleArray.retainCount==71)||(ScaleArray.retainCount==82)||(ScaleArray.retainCount==94)||(ScaleArray.retainCount==95)||(ScaleArray.retainCount==106)||(ScaleArray.retainCount==107)||(ScaleArray.retainCount==118)||(ScaleArray.retainCount==119))&&(ChordCount.retainCount==3))||
        (((ScaleArray.retainCount==11)||(ScaleArray.retainCount==12)||(ScaleArray.retainCount==13)||(ScaleArray.retainCount==23)||(ScaleArray.retainCount==24)||(ScaleArray.retainCount==35)||(ScaleArray.retainCount==36)||(ScaleArray.retainCount==37)||(ScaleArray.retainCount==48)||(ScaleArray.retainCount==60)||(ScaleArray.retainCount==61)||(ScaleArray.retainCount==72)||(ScaleArray.retainCount==83)||(ScaleArray.retainCount==84)||(ScaleArray.retainCount==96)||(ScaleArray.retainCount==97)||(ScaleArray.retainCount==108)||(ScaleArray.retainCount==109)||(ScaleArray.retainCount==120))&&(ChordCount.retainCount==2))) {

    if ([KeyCountA2 containsObject:[NSString stringWithFormat:@"%d",array1.retainCount]])//A2
    {if(array1.retainCount+1>Max){[S1S sendActionsForControlEvents:UIControlEventTouchDown];}else{[array1 retain];}}

    else if ([KeyCountA1 containsObject:[NSString stringWithFormat:@"%d",array1.retainCount]])//A1
    {if(array1.retainCount+2>Max){[S1S sendActionsForControlEvents:UIControlEventTouchDown];}else{[array1 retain];[array1 retain];}}

    else if ([KeyCountC7 containsObject:[NSString stringWithFormat:@"%d",array1.retainCount]])
    {if(array1.retainCount+3>Max){[S1S sendActionsForControlEvents:UIControlEventTouchDown];}else{[array1 retain];[array1 retain];[array1 retain];}}//C7
}//Phase#1

if ((((ScaleArray.retainCount==1)||(ScaleArray.retainCount==2)||(ScaleArray.retainCount==14)||(ScaleArray.retainCount==25)||(ScaleArray.retainCount==26)||(ScaleArray.retainCount==38)||(ScaleArray.retainCount==49)||(ScaleArray.retainCount==50)||(ScaleArray.retainCount==62)||(ScaleArray.retainCount==73)||(ScaleArray.retainCount==74)||(ScaleArray.retainCount==85)||(ScaleArray.retainCount==86)||(ScaleArray.retainCount==98)||(ScaleArray.retainCount==110)||(ScaleArray.retainCount==111))&&(ChordCount.retainCount==2))||
    (((ScaleArray.retainCount==3)||(ScaleArray.retainCount==4)||(ScaleArray.retainCount==15)||(ScaleArray.retainCount==16)||(ScaleArray.retainCount==27)||(ScaleArray.retainCount==28)||(ScaleArray.retainCount==39)||(ScaleArray.retainCount==40)||(ScaleArray.retainCount==51)||(ScaleArray.retainCount==52)||(ScaleArray.retainCount==63)||(ScaleArray.retainCount==64)||(ScaleArray.retainCount==75)||(ScaleArray.retainCount==76)||(ScaleArray.retainCount==87)||(ScaleArray.retainCount==88)||(ScaleArray.retainCount==99)||(ScaleArray.retainCount==100)||(ScaleArray.retainCount==112))&&(ChordCount.retainCount==1))||
    (((ScaleArray.retainCount==5)||(ScaleArray.retainCount==17)||(ScaleArray.retainCount==18)||(ScaleArray.retainCount==29)||(ScaleArray.retainCount==30)||(ScaleArray.retainCount==41)||(ScaleArray.retainCount==42)||(ScaleArray.retainCount==53)||(ScaleArray.retainCount==54)||(ScaleArray.retainCount==65)||(ScaleArray.retainCount==66)||(ScaleArray.retainCount==77)||(ScaleArray.retainCount==89)||(ScaleArray.retainCount==101)||(ScaleArray.retainCount==102)||(ScaleArray.retainCount==113)||(ScaleArray.retainCount==114))&&(ChordCount.retainCount==7))||
    (((ScaleArray.retainCount==6)||(ScaleArray.retainCount==7)||(ScaleArray.retainCount==19)||(ScaleArray.retainCount==31)||(ScaleArray.retainCount==43)||(ScaleArray.retainCount==55)||(ScaleArray.retainCount==67)||(ScaleArray.retainCount==68)||(ScaleArray.retainCount==78)||(ScaleArray.retainCount==79)||(ScaleArray.retainCount==90)||(ScaleArray.retainCount==91)||(ScaleArray.retainCount==103)||(ScaleArray.retainCount==115)||(ScaleArray.retainCount==116))&&(ChordCount.retainCount==6))||
    (((ScaleArray.retainCount==8)||(ScaleArray.retainCount==9)||(ScaleArray.retainCount==20)||(ScaleArray.retainCount==21)||(ScaleArray.retainCount==32)||(ScaleArray.retainCount==33)||(ScaleArray.retainCount==44)||(ScaleArray.retainCount==45)||(ScaleArray.retainCount==56)||(ScaleArray.retainCount==57)||(ScaleArray.retainCount==69)||(ScaleArray.retainCount==80)||(ScaleArray.retainCount==81)||(ScaleArray.retainCount==92)||(ScaleArray.retainCount==93)||(ScaleArray.retainCount==104)||(ScaleArray.retainCount==105)||(ScaleArray.retainCount==117))&&(ChordCount.retainCount==5))||
    (((ScaleArray.retainCount==10)||(ScaleArray.retainCount==22)||(ScaleArray.retainCount==34)||(ScaleArray.retainCount==46)||(ScaleArray.retainCount==47)||(ScaleArray.retainCount==58)||(ScaleArray.retainCount==59)||(ScaleArray.retainCount==70)||(ScaleArray.retainCount==71)||(ScaleArray.retainCount==82)||(ScaleArray.retainCount==94)||(ScaleArray.retainCount==95)||(ScaleArray.retainCount==106)||(ScaleArray.retainCount==107)||(ScaleArray.retainCount==118)||(ScaleArray.retainCount==119))&&(ChordCount.retainCount==4))||
    (((ScaleArray.retainCount==11)||(ScaleArray.retainCount==12)||(ScaleArray.retainCount==13)||(ScaleArray.retainCount==23)||(ScaleArray.retainCount==24)||(ScaleArray.retainCount==35)||(ScaleArray.retainCount==36)||(ScaleArray.retainCount==37)||(ScaleArray.retainCount==48)||(ScaleArray.retainCount==60)||(ScaleArray.retainCount==61)||(ScaleArray.retainCount==72)||(ScaleArray.retainCount==83)||(ScaleArray.retainCount==84)||(ScaleArray.retainCount==96)||(ScaleArray.retainCount==97)||(ScaleArray.retainCount==108)||(ScaleArray.retainCount==109)||(ScaleArray.retainCount==120))&&(ChordCount.retainCount==3))) {

    if ([KeyCountA3 containsObject:[NSString stringWithFormat:@"%d",array1.retainCount]])
    {if(array1.retainCount+1>Max){[S1S sendActionsForControlEvents:UIControlEventTouchDown];}else{[array1 retain];}}

    else if ([KeyCountA2 containsObject:[NSString stringWithFormat:@"%d",array1.retainCount]])//A1
    {if(array1.retainCount+2>Max){[S1S sendActionsForControlEvents:UIControlEventTouchDown];}else{[array1 retain];[array1 retain];}}

    else if ([KeyCountC1 containsObject:[NSString stringWithFormat:@"%d",array1.retainCount]])
    {if(array1.retainCount+3>Max){[S1S sendActionsForControlEvents:UIControlEventTouchDown];}else{[array1 retain];[array1 retain];[array1 retain];}}//C7
}//Phase#2

//跳过阶段#3-7

[ChangeScale sendActionsForControlEvents:UIControlEventTouchUpInside];


[label1 setText:[NSString stringWithFormat:@"%d = %@", array1.retainCount, [[KeyName objectAtIndex:array1.retainCount]description]]];


NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:[NSString stringWithFormat:@"%@",[[Scale objectAtIndex:array1.retainCount]description]]
                                  ofType:@"mp3"
                             inDirectory:@"Piano"];

NSURL *url = [NSURL fileURLWithPath: path];
player = [[AVAudioPlayer alloc]
          initWithContentsOfURL:url error:nil];
[player setNumberOfLoops:0];
[player play];

[EnableButton sendActionsForControlEvents:UIControlEventTouchUpInside];
[CCC sendActionsForControlEvents:UIControlEventTouchUpInside];
[EDCButtons sendActionsForControlEvents:UIControlEventTouchUpInside];

}

*note: "Scale" and "KeyName" are arrays** enabled differently according to ScaleArray.retainCount.
4

2 回答 2

12

除了说您确实需要重新审视内存管理的基础知识之外,没有足够的代码来说明可能发生的情况。

retain连续三个s?没有意义,并且表示猜测正在发生的事情。

retainCount没用;不要叫它。 http://www.whentouseretaincount.com/

您是否尝试过在您的代码上运行静态分析器?


从您的评论中,我可以说我真的做错了一切。在我的代码中,我使用不同的 retainCount 数字来表示钢琴键盘上的不同键......因为我不知道如何使用在我的所有 IBAction 中运行的 int 或整数值

我的道歉——这不是要严厉的!

我们都曾一次做错一切。用于retainCount存储 UI 状态实际上是我见过的对该 API 最具创造性的滥用之一!所以,不,你绝对不傻,只是走错了路!

听起来您需要退后一步,看看应用程序是如何组合在一起的。在这种情况下,您有 N 个单独的按钮,每个按钮触发几乎相同的操作。因此,您需要将一些数据附加到这些按钮。

如果按钮是在 Interface Builder 中创建的,您可以为每个按钮分配一个标签。如果它们是以编程方式创建的,您也可以这样做。NSButton 和 UIButton 都有标签,因此您可以在两个平台上使用相同的解决方案。

如果不知道更多关于按钮是如何实现的,很难更具体。

于 2013-05-30T15:32:43.033 回答
1

我认为您可能正在分配多个 avplayer 对象。使用后使其为零并检查播放器的错误,如下所示

nserror *错误;player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

并记录错误

于 2013-05-31T15:48:43.220 回答