我试图点击按钮播放声音。但是当我运行应用程序时它崩溃了,我得到了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];
}
答案 0 :(得分: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
答案 1 :(得分:0)
看看下面显示的链接,并检查一下你犯的错误?
http://looksok.wordpress.com/2013/01/19/ios-tutorial-play-sound/
http://www.techotopia.com/index.php/Playing_Audio_on_an_iPhone_using_AVAudioPlayer_(iOS_6)
http://code-and-coffee.blogspot.in/2012/09/how-to-play-audio-in-ios.html
答案 2 :(得分: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];