0

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

1 回答 1

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复制代码和想法using 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];
}
}
于 2012-09-08T19:15:57.353 回答