2012-05-03 36 views
3

我目前想知道如何在iOS中录制音频。我知道很多人把这个理解为从麦克风录音并回放,但事实并非如此。我正在为iPad制作录音应用程序。在苹果公司在iOS应用商店中的GarageBand应用程序中,您可以录制自己的声音并从应用程序中播放它们。如果这没有任何意义,可以这样想:如何在iOS中录制声音?

我想要做的就是制作一个播放声音的按钮。我需要知道如何录制该按钮声音并能够播放声音序列。因此,如果我按下“录制”,然后按“A,F,J”然后“停止”,然后按“播放”,它将播放录制内容(发出A F和J)。

我想让它可以在这个应用程序中录制和制作自己的音乐。对不起,如果这是令人困惑的,请尽我所能帮助你。谢谢!

+0

你有做过任何研究吗? –

+0

http://stackoverflow.com/questions/4215180/record-and-play-audio-simultaneously的可能副本|查看Apple的aurioTouch示例应用程序以获取示例代码。 –

回答

1

您可以创建两个NSMutableArrays,并在记录时将它们清空。你还需要一个NSTimer和一个int。所以在标题中:

NSTimer *recordTimer; 
NSTimer *playTimer; 
int incrementation; 
NSMutableArray *timeHit; 
NSMutableArray *noteHit; 

在你的头文件中包含所有的空白和IBAction等。

让你的声音按钮都有不同的唯一标签。

,然后在你的主文件:

-(void)viewDidLoad { 

    timeHit = [[NSMutableArray alloc] init]; 
    noteHit = [[NSMutableArray alloc] init]; 

} 

-(IBAction)record { 

    recordTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(timerSelector) userInfo:nil repeats:YES]; 
    [timeHit removeAllObjects]; 
    [noteHit removeAllObjects]; 
    incrementation = 0; 
} 

-(void)timerSelector { 

    incrementation += 1; 

} 

-(IBAction)hitSoundButton:(id)sender { 

    int note = [sender tag]; 
    int time = incrementation; 

    [timeHit addObject:[NSNumber numberWithInt:time]]; 
    [noteHit addObject:[NSNumber numberWithInt:note]]; 
    [self playNote:note]; 
} 

-(IBAction)stop { 

    if ([recordTimer isRunning]) { 

     [recordTimer invalidate]; 
    } else if ([playTimer isRunning]) { 

     [playTimer invalidate]; 
    } 

} 

-(IBAction)playSounds { 

    playTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(playback) userInfo:nil repeats:YES]; 

    incrementation = 0; 



} 


-(void)playback { 

    incrementation += 1; 

    if ([timeHit containsObject:[NSNumber numberWithInt:incrementation]]) { 

     int index = [timeHit indexOfObject:[NSNumber numberWithInt:incrementation]]; 

     int note = [[noteHit objectAtIndex:index] intValue]; 

     [self playNote:note]; 
    } 
} 


-(void)playNote:(int)note { 


    //These notes would correspond to the tags of the buttons they are played by. 

    if (note == 1) { 
     //Play your first note 
    } else if (note == 2) { 
     //Play second note 
    } else if (note == 3) { 
     //And so on 
    } else if (note == 4) { 
      //etc. 
    } 

} 

乱搞(我怀疑这个代码是完美的)的一点点,你可能会得到这个工作。就像你可能想让播放/录制按钮在你点击其中一个时被禁用。祝你好运!