こんにちは、シミュレーターで非常に奇妙な問題が発生しましたが、アプリを iPad に充電すると機能しません。何か案が?
ここでは、すべての変数を作成して機能させます。
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
}
録音、再生、停止用のボタンがいくつかあります。したがって、これらはすべてのコードです。
-(void) recordAudio{
if (!audioRecorder.recording){
recordButton.enabled = NO;
playButton.enabled = NO;
saveButton.enabled = NO;
stopButton.enabled = YES;
[audioRecorder record];
}
}
-(void)stop{
recordButton.enabled = YES;
playButton.enabled = YES;
saveButton.enabled = YES;
stopButton.enabled = NO;
if (audioRecorder.recording){
[audioRecorder stop];
}
else if (audioPlayer.playing){
[audioPlayer stop];
}
}
-(void) playAudio{
if (!audioRecorder.recording){
recordButton.enabled = NO;
stopButton.enabled = YES;
saveButton.enabled = NO;
if (audioPlayer)
audioPlayer=nil;
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:audioRecorder.url
error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(@"Error: %@", [error localizedDescription]);
else
[audioPlayer play];
}
}