对 Objective-C 和 Xcode 编程来说非常新,我不太明白为什么我的声音没有播放。我遵循了本教程(http://www.youtube.com/watch?v=2mbCkIVF1-0),但由于某种原因,我无法在 iOS 模拟器中播放我的声音。我使用的是故事板布局而不是 nib 文件,所以这可能是问题所在?我知道如何将我的按钮连接到我拥有的故事板,所以我很困惑,这个问题让我发疯。非常感谢任何帮助,如果我的代码错误,请指出正确的方向,问题可能是微不足道的,无论哪种方式,任何帮助都会很棒!再次感谢
SFViewController.m
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface SFViewController : UIViewController <AVAudioPlayerDelegate> {
}
-(IBAction)playSound1;
-(IBAction)playSound2;
-(IBAction)playSound3;
@end
SFViewController.h
#import "SFViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
@implementation SFViewController
-(IBAction)playSound1 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound1" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate = self;
[theAudio play];
}
-(IBAction)playSound2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound2" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate = self;
[theAudio play];
}
-(IBAction)playSound3 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"SeriouslyFunnySound3" ofType:@"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate = self;
[theAudio play];
}