簡體   English   中英

簡單的節拍器iPhone應用程序

[英]Simple Metronome iPhone App

我正在嘗試構建一個簡單的節拍器應用程序,我想知道是否有任何示例代碼或開源項目可供學習。 我認為蘋果曾經擁有它,但現在沒有了。 我認為應該不難,但我很好奇如何加載音頻,如何設置計時器並相應地循環音頻。 任何幫助,不勝感激。

蘋果的Metronome App仍可在iOS 4.2庫中使用。

在Xcode中,只需轉到Window > Organizer
然后轉到“ Documentation窗格並搜索“ Metronome
節拍器項目將出現在“示例代碼”部分下。

您可以轉到“首選項”->“下載”->“文檔”,並確保列表中包含iOS 4.2庫,以確保擁有iOS 4.2庫。

所以...截至2015年夏季,蘋果對其網站的重新設計似乎已經破壞了這些鏈接。 我找到了.xar格式的摘要鏈接http://devimages.apple.com/docsets/20101122/com.apple.adc.documentation.AppleiOS4_2.iOSLibrary.Xcode4.xar ,您可以下載該文件,然后使用xar -xf <docsetfilename>進行解壓縮xar -xf <docsetfilename>命令行工具或其他類似unarchiver應用程序的工具。

我嘗試過NSTimer,但如果您正在尋找Pro Metronome,則它不是一個好的解決方案。 您需要一個核心引擎來推動時間到達所需的位置。 NSTimer使您可以在無法達到所需精度的時間空間中循環。

現在看,iOS 5允許您使用Music Sequencer,這是Musical Apps的良好解決方案。 並且它具有控制時間的核心引擎。

僅出於Google的目的,這就是我對此的發現。 我已經嘗試了Apple示例方法(使用后台線程)和NSTimer方法,而迄今為止的成功者是使用線程。 只是沒有一種方法可以使NSTimer在主(UI)線程上運行時足夠准確地啟動。 我想您可能會花一些時間在后台運行,但是Apple的示例確實運行得很好。

這是我之前制作的一個節拍器項目,它相當簡單,但應該會有所幫助。如果使用它,只需引用我,Jordan Brown 15y Mango Apps。 花了一段時間,但從未開發出一款應用。

        //.h
NSTimer *timer;
int count;
float bpm;
float speed;
UILabel *numberLabel;
IBOutlet UISwitch *vibrate;
IBOutlet UISegmentedControl *timing;
 }
 - (IBAction)up;
 - (IBAction)down;
 - (IBAction)stop:(id)sender;
 @property (nonatomic, retain)IBOutlet UILabel *numberLabel;
 @property (nonatomic, retain)IBOutlet UILabel *bpmLabel;

 @property (nonatomic, retain)IBOutlet UISegmentedControl *timing;



 //.m
  #define SECONDS 60
  #import <AudioToolbox/AudioToolbox.h>

 @implementation metronome
  @synthesize numberLabel; // labels
  @synthesize bpmLabel;
  @synthesize timing; 

-(IBAction)stop:(id)sender{
[timer invalidate];
[self performSelector:@selector(playTockSound)];
numberLabel.text = [NSString stringWithFormat:@"%i",count];
bpm = bpm;
if (bpm > 300) {
    bpm = 300;
}
int new = bpm;

bpmLabel.text = [NSString stringWithFormat:@"%i",new];
speed = INFINITY;

NSLog(@"%f",speed);
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(updateNumber) userInfo:nil repeats:YES];

}
 -(IBAction)up{
 [timer invalidate];
count = 1;
[self performSelector:@selector(playTockSound)];
numberLabel.text = [NSString stringWithFormat:@"%i",count];
bpm = bpm+10;
if (bpm > 300) {
    bpm = 300;
}
int new = bpm;

bpmLabel.text = [NSString stringWithFormat:@"%i",new];
speed = SECONDS/bpm;

NSLog(@"%f",speed);
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(updateNumber) userInfo:nil repeats:YES];
 }
-(IBAction)down{
 [timer invalidate];
count = 1;
[self performSelector:@selector(playTockSound)];
numberLabel.text = [NSString stringWithFormat:@"%i",count];
bpm = bpm-10;
if (bpm < 10) {
    bpm = 10;
}
int new = bpm;

bpmLabel.text = [NSString stringWithFormat:@"%i",new];
speed = SECONDS/bpm;
NSLog(@"%f",speed);
timer = [NSTimer scheduledTimerWithTimeInterval:SECONDS/bpm target:self               selector:@selector(updateNumber) userInfo:nil repeats:YES];

  }
  -(void)updateNumber{
count += 1;
//if 4/4 timing is selected then the count wont go past 4
if (timing.selectedSegmentIndex == 2) {
if (count >= 5) {
    count = 1;
}
}

//if 3/4 timing is selected then the count wont go past 3
if (timing.selectedSegmentIndex == 1) {
    if (count >= 4) {
        count = 1;
    }
}

//if 2/4 timing is selected then the count wont go past 2
if (timing.selectedSegmentIndex == 0) {
    if (count >= 3) {
        count = 1;
    }
}    
  //In each timing case it plays the sound on one and depending 
  //on the limitiations on the cont value the amount of each tick 
      if (count == 1) {
    [self performSelector:@selector(playTockSound)];
}else {
    [self performSelector:@selector(playTickSound)];
}

numberLabel.text = [NSString stringWithFormat:@"%i",count];
  }
  -(void)playTickSound
  {
NSString *path = [[NSBundle mainBundle] pathForResource:@"tick" 
                                                 ofType:@"caf"];
SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                 , &soundID);
  AudioServicesPlaySystemSound (soundID);




  }
  -(void)playTockSound
  {
NSString *path = [[NSBundle mainBundle] pathForResource:@"tock" 
                                                 ofType:@"caf"];

SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                 , &soundID);
     AudioServicesPlaySystemSound (soundID);



  - (void)didReceiveMemoryWarning
  {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
  }


  - (void)viewDidLoad
  {
bpm = 60.00;
speed = SECONDS/bpm;
timer = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(updateNumber) userInfo:nil repeats:YES];
int new = bpm;

bpmLabel.text = [NSString stringWithFormat:@"%i",new];
[super viewDidLoad];

  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM