簡體   English   中英

節拍器聲音的AVPlayer,AVAudioPlayer和AVRecord無法在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:不推薦使用AudioSessionInitialize和AudioSessionSetProperty

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM