2014-09-21 85 views
-2

這是我的.m文件中的內容,來自xcode。 我的主要目標是有一個按鈕播放聲音(不是在同一時間),當它有2個聲音文件附加和一個隨機發生器,因此它不會播放兩次相同的聲音或不會做兩次。我運行代碼,當我點擊Yes按鈕時,發生lldb NSEsception錯誤。香港專業教育學院環顧四周的LLDB異常錯誤,但我找到了解決辦法是進入視圖 - 控制器的帶有黃色感嘆號刪除任何東西;它沒有不能解決問題..由於NSEexception而終止?

 #import "ViewController.h" 
     #import <AVFoundation/AVAudioPlayer.h> 

@interface ViewController() 
@end 
    @implementation ViewController : UIViewController 
    - (void)viewDidLoad 
{ 


[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
    } 

    - (void)didReceiveMemoryWarning 
    { 

    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
    } 

    - (IBAction)Yes{ 
    NSMutableArray *array=[NSMutableArray 
         arrayWithObjects: 
         @"yes.mp3", 
         @"yes 3.mp3", 

         nil]; 

int i=0; 
for(i=0;i<=[array count]; i++) 
    { 

    NSInteger rand=(arc4random() %1); 
    [array exchangeObjectAtIndex:i 
       withObjectAtIndex:rand]; 
    } 
    NSString *sound = [array objectAtIndex:i]; 
    AVAudioPlayer* audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:   

    [[NSBundle mainBundle]pathForResource:sound ofType:@"mp3"]] error:NULL]; 


    [audioPlayer play]; 
    } 

@end 
+1

http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 – Paulw11 2014-09-21 22:32:45

回答

0

你有四個方面的問題。循環範圍,隨機值的計算以及循環後的數組索引使用情況,並指定聲音文件的擴展名兩次。

循環和隨機值應爲:

for(NSUInteger i = 0; i < [array count]; i++) { 
    NSInteger rand = arc4random_uniform(array.count); 
    [array exchangeObjectAtIndex:i withObjectAtIndex:rand]; 
} 
  1. 通知的<=現在<
  2. 你的隨機數的計算總是返回0。

目前還不清楚你想要得到的循環之後的sound價值是什麼。正如所寫的,你嘗試使用i來訪問,這將會太大。您需要指定範圍0array.count - 1之間的值。

如果你只想1日從陣列,使用(現隨機)值:

NSString *sound = [array firstObject]; 

更新:

實現你真的只是想打兩個聲音的一個隨機後我覺得你的代碼可以簡化爲:

- (IBAction)Yes { 
    NSInteger rand = arc4random_uniform(2); 
    NSString sound = rand == 0 ? @"yes" : @"yes 3"; 
    NSURL *url = [[NSBundle mainBundle] URLForResource:sound withExtension:@"mp3"]; 

    AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    [audioPlayer play]; 
} 
+0

我該在pathforresource中指定這個值?因爲我們的目標是讓按鈕變亮,然後聲音被播放1或2,然後再次點擊1或2(因爲它的2個聲音),所以它有點隨機。 – cloudhalo 2014-09-21 22:45:22

+0

仍然得到例外。將聲音放入Pathforresource中可以嗎? – cloudhalo 2014-09-21 22:50:56

+0

查看我的更新。這很容易。另外,請學習在代碼中使用一些空格,以便於閱讀。 – rmaddy 2014-09-21 22:52:08

1

for(i=0;i<=[array count]; i++)

應該成爲

for(i=0;i<[array count]; i++)

否則你走不出數組邊界。

而且你還要仔細檢查值rand變量的範圍,看看它從來沒有超過數組大小 - 1 所以例如:

NSInteger rand=MIN((arc4random() %1), array.count - 1);

+0

仍然得到點擊按鈕後出現錯誤。我認爲這與在資源路徑中使用字符串聲音有關,但我不確定還有什麼要放在路徑中,如果聲音字符串沒有去那裏告訴avaudio數組的內容是什麼所以它可以播放? – cloudhalo 2014-09-21 22:35:07

+0

你能粘貼完整的異常嗎? @cloudhalo – 2014-09-21 22:36:22

+0

這裏是完整的例外2014-09-21 18:33:12.377 Strogg [59125:90b] ***由於未捕獲異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引2超越界限[0 ..1]」 ***第一擲調用堆棧: ( \t 的libC++ abi.dylib:與類型NSException的未捕獲的異常 (LLDB) – cloudhalo 2014-09-21 22:38:08

相關問題