2013-10-15 83 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]; 
相关问题