iOS,我的iPad没有录音,在模拟器中工作正常

时间:2012-09-07 03:07:48

标签: ios ipad audio

嗨我在模拟器中有一个非常奇怪的问题,但是当我将应用程序充电到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];
}
}

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。在我之前的代码中,我在ViewdidLoad中添加了这些行。

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

对于我添加的设置:

[NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey

我从http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1复制代码,使用AVAudioSession的想法来自How to record and play sound in iPhone app?

如果您需要完整的代码就是这个。

- (void)viewDidLoad {

[super viewDidLoad];

NSError *error = nil;

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

playButton.enabled = NO;
stopButton.enabled = NO;
saveButton.enabled = NO;

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
docsDir = [HSAudioController pathToDocumentsFolder];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey,
                                [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16], AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                nil];

audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL
                                            settings:recordSettings
                                               error:&error];
NSLog(@"%@",audioRecorder.url);
if (error)
{
    NSLog(@"error: %@", [error localizedDescription]);
} else {
    [audioRecorder prepareToRecord];
}
}