繁体   English   中英

录音机无法在iphone 7的iphone中工作

[英]Audio Recorder not working in iphone with ios 7

我面临着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];

下面是我的超级简单演示的完整代码。 AudioPlayer和AudioRecorder在.h中被调出

#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM