根据iPhone / xcode上按下的按钮播放声音

时间:2010-04-14 21:55:29

标签: iphone xcode audio avaudioplayer

我正在尝试根据使用AVAudioPlayer按下哪个按钮播放声音。 (这不是音板或放屁应用程序。)

我已在头文件中使用此代码链接所有按钮:

@interface appViewController : UIViewController <AVAudioPlayerDelegate> {

AVAudioPlayer *player;

UIButton *C4;
UIButton *Bb4;
UIButton *B4;
UIButton *A4;
UIButton *Ab4;
UIButton *As4;
UIButton *G3;
UIButton *Gb3;
UIButton *Gs3;
UIButton *F3;
UIButton *Fs3;
UIButton *E3;
UIButton *Eb3;
UIButton *D3;
UIButton *Db3;
UIButton *Ds3;
UIButton *C3;
UIButton *Cs3;
}

@property (nonatomic, retain) AVAudioPlayer *player;

@property (nonatomic, retain) IBOutlet UIButton *C4;
@property (nonatomic, retain) IBOutlet UIButton *B4;
@property (nonatomic, retain) IBOutlet UIButton *Bb4;
@property (nonatomic, retain) IBOutlet UIButton *A4;
@property (nonatomic, retain) IBOutlet UIButton *Ab4;
@property (nonatomic, retain) IBOutlet UIButton *As4;
@property (nonatomic, retain) IBOutlet UIButton *G3;
@property (nonatomic, retain) IBOutlet UIButton *Gb3;
@property (nonatomic, retain) IBOutlet UIButton *Gs3;
@property (nonatomic, retain) IBOutlet UIButton *F3;
@property (nonatomic, retain) IBOutlet UIButton *Fs3;
@property (nonatomic, retain) IBOutlet UIButton *E3;
@property (nonatomic, retain) IBOutlet UIButton *Eb3;
@property (nonatomic, retain) IBOutlet UIButton *D3;
@property (nonatomic, retain) IBOutlet UIButton *Db3;
@property (nonatomic, retain) IBOutlet UIButton *Ds3;
@property (nonatomic, retain) IBOutlet UIButton *C3;
@property (nonatomic, retain) IBOutlet UIButton *Cs3;

- (IBAction) playNote;

@end

按钮全部链接到interfaceBuilder中的事件“playNote”,并且每个音符都根据音符名称链接到正确的引用插座。

所有* .mp3声音文件都以UIButton名称命名(IE-C3 == C3.mp3)。

在我的实现文件中,我按下C3按钮时只有一个音符:

#import "sonicfitViewController.h"

@implementation appViewController
@synthesize C3, Cs3, D3, Ds3, Db3, E3, Eb3, F3, Fs3, G3, Gs3, A4, Ab4, As4, B4, Bb4, C4;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"3C" ofType:@"mp3"];
    NSLog(@"path: %@", path);
    NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:file error:nil];
    [file release];

    self.player = p;
    [p release];

    [player prepareToPlay];
    [player setDelegate:self];

    [super viewDidLoad];
}

- (IBAction) playNote {
    [self.player play];
}

现在,有了上述内容,我有两个问题:

  • 首先,NSLog报告NULL并在尝试播放文件时崩溃。我已经将mp3添加到资源文件夹中,它们已被复制而不仅仅是链接。它们不在资源文件夹下的子文件夹中。
  • 其次,如何设置它以便当按下按钮C3时,它会播放C3.mp3并且F3播放F3.mp3而不为每个不同的按钮写入重复的代码行? playNote应该像

    NSString * path = [[NSBundle mainBundle] pathForResource:nameOfButton ofType:@“mp3”];

而不是专门定义它(@“C3”)。

有没有更好的方法来执行此操作以及为什么*路径报告NULL并在加载应用时崩溃?

我很确定这就像添加额外的变量输入一样简单 - (IBAction)playNote:buttonName并将所有代码放在playNote函数中调用AVAudioPlayer,但我不确定代码是这样做的。< / p>

1 个答案:

答案 0 :(得分:0)

使用相同的选择器执行多个操作是一项常见任务。只需为每个按钮设置一个标签属性,并使用NSStrings的NSArray来存储文件名。您甚至无需单独创建每个按钮。有关类似问题problem with selector in iphone?的答案,请参阅代码示例。

检查您的文件是否真的被复制到bundle。例如在模拟器中 使用NSLog记录您的bundle目录(@“myBundle:%@”,[[NSBundle mainBundle] bundlePath]); 并查看文件。