//
//  VoiceWaveView.m
//  Huangshan
//
//  Created by xiejiangbo on 2018/12/6.
//  Copyright © 2018 yin chen. All rights reserved.
//

#import "VoiceWaveView.h"
#import <AVFoundation/AVFoundation.h>
#import "YSCVoiceWaveView.h"


@interface VoiceWaveView ()
@property (nonatomic, strong) AVAudioRecorder *recorder;
@property (nonatomic,strong) UIView *voiceWaveParentView;
@property (nonatomic, strong) YSCVoiceWaveView *voiceWaveView;
@property (nonatomic, strong) NSTimer *updateVolumeTimer;

@end

@implementation VoiceWaveView
+ (VoiceWaveView *)ShareVoiceWaveView{
    static VoiceWaveView *voiceWaveView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        voiceWaveView = [[VoiceWaveView alloc]init];
//        [voiceWaveView initView];
    });
    return voiceWaveView;
}
-(UIView *)getBagView{
    return self.voiceWaveParentView;
}
-(void)StartVoice{
    BOOL seccess = [_recorder record];
    NSLog(@"开始是否成功: %d", seccess);
    self.voiceWaveParentView.hidden = NO;
    [[NSRunLoop currentRunLoop] addTimer:self.updateVolumeTimer forMode:NSRunLoopCommonModes];
}
-(void)EndVoice{
//    [_voiceWaveView removeFromParent];
//    _voiceWaveView = nil;
    self.voiceWaveParentView.hidden = YES;
    [_recorder stop];
    [_updateVolumeTimer invalidate];
    _updateVolumeTimer = nil;
}
-(void)initView{
    [self setupRecorder];
    self.voiceWaveParentView.hidden = YES;
//    [self.view insertSubview:self.voiceWaveParentView atIndex:0];
    [self.voiceWaveView showInParentView:self.voiceWaveParentView];
    [self.voiceWaveView startVoiceWave];
//    [[NSRunLoop currentRunLoop] addTimer:self.updateVolumeTimer forMode:NSRunLoopCommonModes];
}

// 音频初始化 与创建
-(void)setupRecorder
{
    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
    NSDictionary *settings = @{AVSampleRateKey:          [NSNumber numberWithFloat: 44100.0],
                               AVFormatIDKey:            [NSNumber numberWithInt: kAudioFormatAppleLossless],
                               AVNumberOfChannelsKey:    [NSNumber numberWithInt: 2],
                               AVEncoderAudioQualityKey: [NSNumber numberWithInt: AVAudioQualityMin]};
    
    NSError *error;
    self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
    if(error) {
        NSLog(@"Ups, could not create recorder %@", error);
        return;
    }
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    if (error) {
        NSLog(@"Error setting category: %@", [error description]);
    }
    [self.recorder prepareToRecord];
    [self.recorder setMeteringEnabled:YES];
//    [self.recorder record];
}
- (YSCVoiceWaveView *)voiceWaveView
{
    if (!_voiceWaveView) {
        self.voiceWaveView = [[YSCVoiceWaveView alloc] init];
    }
    return _voiceWaveView;
}
// 声波纹创建
- (UIView *)voiceWaveParentView
{
    if (!_voiceWaveParentView) {
        self.voiceWaveParentView = [[UIView alloc] init];
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        _voiceWaveParentView.frame = CGRectMake(0, 0, screenSize.width, 100);
//        _voiceWaveParentView.backgroundColor = [UIColor yellowColor];
    }
    return _voiceWaveParentView;
}
- (NSTimer *)updateVolumeTimer
{
    if (!_updateVolumeTimer) {
        self.updateVolumeTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateVolume:) userInfo:nil repeats:YES];
    }
    
    return _updateVolumeTimer;
}
- (void)updateVolume:(NSTimer *)timer
{
    [self.recorder updateMeters];
    CGFloat normalizedValue = pow (10, [self.recorder averagePowerForChannel:0] / 20);
    [_voiceWaveView changeVolume:normalizedValue];
    
    //    [_voiceWaveViewNew changeVolume:normalizedValue];
}

@end
