2010-04-14 78 views
1

我試圖播放基於哪個按鈕使用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」 InterfaceBuilder下並且每個音符根據音符名稱鏈接到正確的參考插座。

所有* .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,但我不確定代碼來做到這一點。

回答

0

使用相同的選擇器執行多個操作是一項常見任務。只需爲每個按鈕設置一個標籤屬性並使用NSStrings的NSArray來存儲文件名稱。你甚至不需要單獨創建每個按鈕。請參閱相似問題的代碼示例problem with selector in iphone?答案。

檢查您的文件是否真的被複制到包中。例如在模擬器 用NSLog記錄你的bundle目錄(@「myBundle:%@」,[[NSBundle mainBundle] bundlePath]); 並查看文件。

+0

標記屬性可以是一個字符串還是它必須是一個數字? – slickplaid 2010-04-15 23:59:58

+1

標記是UIView(和所有後代包括UIButton)類的整數屬性。要使用任何其他對象(例如NSString)作爲標籤,請創建此類對象的NSArray,並使用標籤作爲索引訪問它們。例如:[myString objectAtIndex:[myButton.tag]]; – Vladimir 2010-04-16 07:30:42