如何停止AVPlayer Ios并删除periodicTimeObserverForInterval

时间:2014-09-25 11:44:18

标签: ios avfoundation avplayer

我在停止AVPlayer方面遇到了一些困难。

我有一种同时录制和播放音乐的方法。我正在使用AVPlayer播放音乐,因为我想使用addPeriodicTimeObserverForInterval函数。我把它设置如下:

    - (IBAction) recordVoice:(id)sender {
    if(!recorder.isRecording){

    //set up the file name to record to
    NSString *recordingLocation = [self createFileName];
    recordingName = recordingLocation;
    NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject],
                               recordingLocation, nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
    recordingURL = outputFileURL;

    // Setup audio session
    session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
                   error:nil];

    // Define the recording settings to record as m4a
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];

    // initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];
    [session setActive:YES error:nil];
    [recorder record];


    // find which song to play and initiate an AVPlayer to play it
    NSString *playerLocation = self.TitleLabel.text;
    NSString *path = [[NSBundle mainBundle] pathForResource:playerLocation ofType:@"m4a"];
        player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:path]];
        lastTime = nil;

    //check where the player is at and update the song lines accordingly
    [player addPeriodicTimeObserverForInterval:CMTimeMake(3, 10) queue:NULL usingBlock:^(CMTime time){
            NSTimeInterval seconds = CMTimeGetSeconds(time);
            for (NSDictionary *item in robotR33) {
                NSNumber *time = item[@"time"];
                if ( seconds > [time doubleValue] && [time doubleValue] >= [lastTime doubleValue] ) {
                    lastTime = @(seconds);
                    NSString *str = item[@"line"];
                    [self nextLine:str];
                };
            }
        }];
        [player play];

    [_recordButton setImage:[UIImage imageNamed:@"micRecording.gif"] forState:UIControlStateNormal];

    }
    else{
        [recorder stop];
        player = nil;
        [session setActive:NO error:nil];
    }   
}

如果录音机没有录音我设置了新的录音机AVAudioRecorder和AVPlayer。在AVPlayer中,我设置了一个AddPeriodicTimeObserverForInterval,它根据播放器的位置更新UI。

如果录音机正在录音我停止录音机并将播放器设置为零。这会停止播放音频,但我注意到addPeriodicTimeObserverInterval仍在运行,因为UI会继续更新。我应该完全破坏AVPlayer,如果是这样,我应该怎么做?非常感谢提前。

另外,我在addPeriodicTimeObserverForInterval块中有一个警告。我正在循环一个名为robotR33的数组。 Xcode告诉我“在这个区块中强烈捕获自我可能会导致保留周期”。这可能是我问题的一部分吗?

1 个答案:

答案 0 :(得分:4)

完成游戏后,观察者需要从玩家中移除。

添加[player removeTimeObserver:self.timeObserver]有效。