[英]My Application crashes when playing a sound
I am trying to play a sound on the click of a button. 我试图单击一个按钮来播放声音。 But when I run the app it crashes and I get Thread 1 : signal SIGABRT
. 但是,当我运行该应用程序时,它崩溃了,我得到Thread 1 : signal SIGABRT
。
This is my code: 这是我的代码:
.h file .h文件
#import <AVFoundation/AVFoundation.h>
@interface HljodViewController : UIViewController <AVAudioPlayerDelegate>{
}
-(IBAction)playsound;
.m file .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];
}
No one can tell you how to solve your issue if you don't capture the NSError
. 如果您不捕获NSError
没人能告诉您如何解决您的问题。 I suggest you do the following: 我建议您执行以下操作:
NSError *outError = nil;
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&outError];
if (outError)
{
NSLog(@"%@", [outError localizedDescription]);
}
... // continue on to happy time
Just have a look at below shown links,and check that where you are committing mistake ? 只需查看下面显示的链接,然后检查您在哪里犯错?
http://looksok.wordpress.com/2013/01/19/ios-tutorial-play-sound/ 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://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 http://code-and-coffee.blogspot.in/2012/09/how-to-play-audio-in-ios.html
try this: 尝试这个:
[[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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.