在iOS7中录制背景播放歌曲

时间:2014-06-26 11:03:27

标签: ios

我正在尝试使用AVAudioRecorder录制播放歌曲的背景。我的代码如下:

//Initialize audio session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

//Override record to mix with other app audio, background audio not silenced on record
UInt32 allowMixing = true;
OSStatus propertySetError = 0;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
NSLog(@"Mixing: %lx", propertySetError); // This should be 0 or there was an issue somewhere


UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);



[[AVAudioSession sharedInstance] setActive:YES error:nil];

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] initWithCapacity:0];

if (recordEncoding == ENC_PCM) {
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]  forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0]              forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt:2]                      forKey:AVNumberOfChannelsKey];

    [recordSetting setValue:[NSNumber numberWithInt:16]                     forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue:[NSNumber numberWithBool:NO]                    forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue:[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];
            break;
    }

    [recordSetting setValue:formatObject                                forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0]          forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt:2]                  forKey:AVNumberOfChannelsKey];
    [recordSetting setValue:[NSNumber numberWithInt:12800]              forKey:AVEncoderBitRateKey];

    [recordSetting setValue:[NSNumber numberWithInt:16]                 forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recDir = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", recDir]];

NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];

if (!audioRecorder) {
    NSLog(@"audioRecorder: %@ %d %@", [error domain], [error code], [[error userInfo] description]);
    return;
}

//    audioRecorder.meteringEnabled = YES;
//
BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable) {
    UIAlertView *cantRecordAlert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: @"Audio input hardware not available"
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [cantRecordAlert show];
    [cantRecordAlert release];
    return;
}

if ([audioRecorder prepareToRecord]) {
    [audioRecorder record];
    NSLog(@"recording");
} else {
    //        int errorCode = CFSwapInt32HostToBig ([error code]);
    //        NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);
    NSLog(@"recorder: %@ %d %@", [error domain], [error code], [[error userInfo] description]);
}

但此处ios7警告中出现的会话发生在:

AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

任何人都可以帮我删除此警告而不更改功能

0 个答案:

没有答案