2013-10-15 115 views
0

我試圖通過單擊按鈕播放聲音。但是,當我運行應用程序崩潰,我得到Thread 1 : signal SIGABRT我的應用程序在播放聲音時崩潰

這是我的代碼:

.h文件中

#import <AVFoundation/AVFoundation.h> 

@interface HljodViewController : UIViewController <AVAudioPlayerDelegate>{ 

} 

-(IBAction)playsound; 

.m文件

-(IBAction)playsound { 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Geese_4" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.numberOfLoops = 1; 
    [theAudio play]; 
} 
+1

新增了[一般異常斷點(https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator /articles/adding_an_exception_breakpoint.html#//apple_ref/doc/uid/TP40010433-CH1-SW1)。 – rckoenes

回答

1

沒有人能告訴你如何解決您的問題,如果你不抓住NSError。我建議你做到以下幾點:

NSError *outError = nil; 
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&outError]; 
if (outError) 
{ 
    NSLog(@"%@", [outError localizedDescription]); 
} 
... // continue on to happy time 
0

試試這個:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
[[AVAudioSession sharedInstance] setActive:YES error:nil]; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourfile" ofType:@"mp3"]; 
NSError *error; 
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
[sound play]; 
相關問題