AVAudioPlayer를 사용하여 어떤 버튼을 눌렀는지 기반으로 사운드를 재생하려고합니다. (이것은 사운드 보드 또는 방귀 응용 프로그램이 아닙니다.)IPhone/xcode에서 누른 버튼을 기반으로 사운드 재생
내가 헤더 파일에이 코드를 사용하여 모든 버튼에 연결 한 :
@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
버튼이 모든 이벤트에 링크 된 "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 파일을 resources 폴더에 추가했는데 파일이 복사되고 링크 된 것은 아닙니다. 리소스 폴더 아래의 하위 폴더에는 없습니다.
둘째, C3 버튼을 눌렀을 때 C3.mp3을 재생하고 F3이 각기 다른 버튼에 중복 코드 줄을 쓰지 않고 F3.mp3을 재생하도록 설정하려면 어떻게해야합니까? playNote는 다음과 같아야합니다
NSString * path = [[NSBundle mainBundle] pathForResource : nameOfButton ofType : @ "mp3"];
(@ "C3")을 특별히 정의하는 대신.
더 좋은 방법이 있습니까? 왜 * 경로가 NULL로보고하고 응용 프로그램을로드 할 때 충돌이 발생합니까?
- (IBAction) playNote : buttonName에 추가 변수 입력을 추가하는 것만 큼 간단하지만 playNote 함수에서 AVAudioPlayer를 호출하는 데 필요한 모든 코드가 있지만이 작업을 수행하는 코드는 확실하지 않습니다. .
태그 속성을 문자열로 표시 할 수 있습니까, 아니면 숫자 여야합니까? – slickplaid
태그는 UIView (및 UIButton을 포함한 모든 자손) 클래스의 정수 속성입니다. 다른 객체 (예 : NSString)를 태그로 사용하려면 해당 객체의 NSArray를 만들고 태그를 색인으로 사용하여 액세스하십시오. 예를 들면 다음과 같습니다. [myString objectAtIndex : [myButton.tag]]; – Vladimir