我面临着AVFoundation框架的问题。我的应用程序已准备好以旧版本制作,然后我这次升级到ios 7,因此我的录音机正在模拟器上运行,但无法在iPhone上运行。所以有人帮助我什么是实际问题。

我的代码是这样

-(IBAction)startRecording
{
    btnRecord.showsTouchWhenHighlighted = YES;
    btnRecord.enabled = NO;
    btnPlay.enabled = NO;
    btnStop.enabled = YES;
    btnPause.enabled = YES;
    btnDelete.enabled = NO;
    if(!flagPause)
    {
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd"];

        NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
        [timeFormat setDateFormat:@"HH.mm.SS"];

        NSDate *now = [[NSDate alloc] init];

        theDate = [dateFormat stringFromDate:now];
        theTime = [timeFormat stringFromDate:now];


        NSString *filename =[NSString stringWithFormat:@"|%@|%@|",theDate,theTime];
        theDate=[[NSString alloc]initWithString:filename];


        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

        *****  i try this one but not working
        //[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        //[audioSession setActive:YES error:nil];


        NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];

        if(recordEncoding == ENC_PCM)
        {
            [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
            [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
            [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
            [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
            [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
            [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
        }
        else
        {
            NSNumber *formatObject;
            switch (recordEncoding)
            {
                case (ENC_AAC):
                    formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
                    break;
                case (ENC_ALAC):
                    formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
                    break;
                case (ENC_IMA4):
                    formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
                    break;
                case (ENC_ILBC):
                    formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
                    break;
                case (ENC_ULAW):
                    formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
                    break;
                default:
                    formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
            }
            [recordSettings setObject:formatObject forKey: AVFormatIDKey];
            [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
            [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
            [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];

            //**************** I try this one ***********************>>>>
            //[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];

            [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

            [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];
        }
        NSURL *url;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Recording"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

        NSString *filePath =[NSString stringWithFormat:@"%@/%@.caf",dataPath,theDate];
        // appiPhone.strATitle = theDate;
        url = [NSURL fileURLWithPath:filePath];
        NSError *error = nil;

        audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];

        if ([audioRecorder prepareToRecord] == YES)
        {
            [audioRecorder record];
        }
        else
        {
            //int errorCode = CFSwapInt32HostToBig ([error code]);
            // NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);
        }
    }
    else
    {
        [audioRecorder record];
        flagPause = NO;
    }
    //appiPhone.intBtnAnsTag = 1;
}

最佳答案

我最近开发了一个非常简单的播放和录制演示(实际上只有3个按钮可以完成您的期望)。最初我有同样的问题:它可以在iOS模拟器中运行,但不能在我的设备上运行。首先,我必须通过转到设置->隐私->麦克风,确保该应用有权使用麦克风

在设置我的录音机之前,我还必须具有以下代码

 AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[session setActive:YES error:nil];

以下是我的 super 简单演示的完整代码。在.h中调出AudioPlayer和AudioRecorder
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [session setActive:YES error:nil];
    NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:44100],AVSampleRateKey,[NSNumber numberWithInt:kAudioFormatAppleLossless], AVFormatIDKey, [NSNumber numberWithInt:1],AVNumberOfChannelsKey, [NSNumber numberWithInt:AVAudioQualityMedium], AVEncoderAudioQualityKey, nil];

    NSURL *audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"audioRecording.m4a"]];

    self.audioRecorder = [[AVAudioRecorder alloc]initWithURL:audioFileURL settings:audioSettings error:nil];

   [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)audioRecord:(id)sender {

    [self.audioRecorder record];
}

- (IBAction)audioStop:(id)sender {
    [self.audioPlayer stop];
    [self.audioRecorder stop];

    NSURL *audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"audioRecording.m4a"]];
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
}

- (IBAction)audioPlay:(id)sender {

    [self.audioPlayer play];
}
@end

07-26 03:20