ios

AVPlayer, AVAudioPlayer and AVRecording with metronome sound is not synchronization together in ios

当我尝试仅在第一次启动并开始录制所有同步时,如果我想再次使用节拍器录制并导入音乐,则不会同步。

    ///////////// Initiate audio session in viewdidload method ////////////

OSStatus result;
result = AudioSessionInitialize(NULL, NULL, NULL, NULL);

UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);

// set preferred buffer size
Float32 preferredBufferSize = .04; // in seconds
result = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferSize), &preferredBufferSize);

AVAudioSession *session =[AVAudioSession sharedInstance];
    [session setCategory: AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error: nil];


// get actuall buffer size
Float32 audioBufferSize;
UInt32 size = sizeof (audioBufferSize);
result = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareIOBufferDuration, &size, &audioBufferSize);

AudioSessionSetActive(false);

///////////////// 结束 ///////////////////////// ///////////////////////

/////////////// 正在播放从设备库导入的音乐 /////////

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:SongURL];
self.audioPlayer_bg = [[AVPlayer alloc] initWithPlayerItem:playerItem];
self.audioPlayer_bg.volume=self.Voulme_import.value;
[self.audioPlayer_bg play];

/////////////////////////////// 结束 ////////// //////////

    //////////// Playing metronome sound based on the user beats//////////////


NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"tick" ofType:@"aif"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
NSError *error;
backgroundMusicPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];

// [backgroundMusicPlayer1 setVolume: 1.0];

 backgroundMusicPlayer1.volume=1.0;

backgroundMusicPlayer1.numberOfLoops=0;
[backgroundMusicPlayer1 play];

////////////// 结束 //////////////////////////// /

////////// Initiate Recording //////////////////////////


        NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *docsDir = [dirPaths objectAtIndex:0];
        soundFilePath = [docsDir stringByAppendingPathComponent:[self timeStamp]];

        NSLog(@"Time Stamp is %@ " , [self timeStamp]);

        NSURL *url = [NSURL fileURLWithPath:soundFilePath];



        NSError *error = nil;
        audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
        audioRecorder.meteringEnabled = YES;
        if ([audioRecorder prepareToRecord] == YES){


            seconds_record=1;


            int minutes =  seconds_record / 60;
            int seconds_remain = (int) seconds_record%60;


        self.Record_timer_lbl.text =[NSString stringWithFormat:@"%d%@%02d",minutes,@":",seconds_remain];

            timer_time_record = [NSTimer scheduledTimerWithTimeInterval:1.0 target: self
                                                               selector: @selector(update_record_time) userInfo: nil repeats: YES];


            NSArray *sound_name=[soundFilePath componentsSeparatedByString:@"/"];
            NSString *str_sound=[sound_name lastObject];


            [self performSelectorOnMainThread:@selector(play_import_song) withObject:self waitUntilDone:NO];



            audioRecorder.meteringEnabled = YES;
            [audioRecorder record];

据我所知,AudioSessionInitializeAudioSessionSetProperty 已弃用。

我建议修改您的代码以使用未弃用的函数,请检查: iOS: Deprecation of AudioSessionInitialize and AudioSessionSetProperty