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

3 回答 3

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
于 2013-10-15T14:36:31.647 回答
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];
于 2013-10-15T14:37:56.467 回答