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からコードをコピーし、 AVAudioSessionの使用は、iPhoneアプリでサウンドを録音および再生する方法から来ましたか?

完全なコードが必要な場合は、これです。

- (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 に答える