根据 iPhone/xcode 上按下的按钮播放声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2641336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play a sound based on the button pressed on the IPhone/xcode
提问by slickplaid
I'm trying to play a sound based on which button is pressed using AVAudioPlayer. (This is not a soundboard or fart app.)
我正在尝试根据使用 AVAudioPlayer 按下的按钮播放声音。 (这不是音板或放屁应用程序。)
I have linked all buttons using this code in the header file:
我在头文件中使用此代码链接了所有按钮:
@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
Buttons are all linked to the event "playNote" in interfaceBuilder and each note is linked to the proper referencing outlet according to note name.
按钮都链接到 interfaceBuilder 中的事件“playNote”,每个音符都根据音符名称链接到正确的引用插座。
All *.mp3 sound files are named after the UIButton name (IE- C3 == C3.mp3).
所有 *.mp3 声音文件都以 UIButton 名称命名(IE-C3 == C3.mp3)。
In my implementation file, I have this to play a only one note when the C3 button is pressed:
在我的实现文件中,当按下 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];
}
Now, with the above I have two issues:
现在,有了上面我有两个问题:
- First, the NSLog reports NULL and crashes when trying to play the file. I have added the mp3's to the resources folder and they have been copied and not just linked. They are not in an subfolder under the resources folder.
Secondly, how can I set it up so that when say button C3 is pressed, it plays C3.mp3 and F3 plays F3.mp3 without writing duplicate lines of code for each different button? playNote should be like
NSString *path = [[NSBundle mainBundle] pathForResource:nameOfButton ofType:@"mp3"];
- 首先,NSLog 在尝试播放文件时报告 NULL 并崩溃。我已将 mp3 添加到资源文件夹中,并且它们已被复制而不仅仅是链接。它们不在资源文件夹下的子文件夹中。
其次,如何设置它以便在按下按钮 C3 时播放 C3.mp3 和 F3 播放 F3.mp3 而不为每个不同的按钮编写重复的代码行?playNote 应该是这样的
NSString *path = [[NSBundle mainBundle] pathForResource:nameOfButton ofType:@"mp3"];
instead of defining it specifically (@"C3").
而不是专门定义它(@“C3”)。
Is there a better way of doing this and why does the *path report NULL and crash when I load the app?
有没有更好的方法来做到这一点,为什么当我加载应用程序时 *path 报告 NULL 并崩溃?
I'm pretty sure it's something as simple as adding additional variable inputs to - (IBAction) playNote:buttonName and putting all the code to call AVAudioPlayer in the playNote function but I'm unsure of the code to do this.
我很确定这很简单,就像向 - (IBAction) playNote:buttonName 添加额外的变量输入并将所有代码放在 playNote 函数中调用 AVAudioPlayer 一样简单,但我不确定执行此操作的代码。
采纳答案by Vladimir
Performing multiple actions with same selector is a common task. Just set a tag property for each button and use a NSArray of NSStrings to store file names. Your even need not create each button separately. See code sample for similar question problem with selector in iphone?answer.
使用同一个选择器执行多个操作是一项常见任务。只需为每个按钮设置一个标签属性,并使用 NSStrings 的 NSArray 来存储文件名。您甚至不需要单独创建每个按钮。有关iphone 中选择器的类似问题,请参阅代码示例?回答。
Check if your files is realy copied to bundle. For example in simulator log your bundle directory with NSLog(@"myBundle:%@",[[NSBundle mainBundle] bundlePath]); and look at files.
检查您的文件是否真的复制到捆绑包中。例如在模拟器中用 NSLog(@"myBundle:%@",[[NSBundle mainBundle] bundlePath]); 记录你的包目录;并查看文件。