【问题标题】:How to record sound in iOS?如何在 iOS 中录制声音?
【发布时间】:2012-05-03 00:11:31
【问题描述】:
我目前想知道如何在 iOS 中录制音频。我知道很多人将其理解为从麦克风录制并播放,但事实并非如此。我正在为 iPad 制作录音应用程序。在 Apple 在 iOS App Store 上的 GarageBand 应用程序中,您可以录制自己的声音并在应用程序中播放。如果这没有意义,可以这样想:
我想做的只是制作一个播放声音的按钮。我需要知道如何录制该按钮的声音并能够播放声音序列。因此,如果我按下“录制”,然后按下“A、F、J”按钮,然后按下“停止”,然后按下“播放”,它将播放录制的内容(声音为 A F 和 J)。
我正在尝试制作它,以便您可以在此应用中录制和制作自己的音乐。抱歉,如果这令人困惑,请尽您的能力帮助我。谢谢!
【问题讨论】:
标签:
ios
ipad
audio-recording
【解决方案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.
}
}
稍微摆弄一下(我怀疑这段代码是否完美),你可能会让它工作。就像您可能希望在按下其中一个按钮后禁用播放/录制按钮一样。祝你好运!