//
//  AudioPlayerPrompt.m
//  Ipadchat
//
//  Created by xiejiangbo on 2017/5/22.
//  Copyright © 2017年 leonchen. All rights reserved.
//
//唤醒提示
//好的,即将为您跳转->待办
//已为您打开流程图页面，请查看网页结果
//已为您发送新增节点请求，请查看网页结果
//已为您发送删除节点请求，请查看网页结果
//已为您发送清空节点请求，请查看网页结果
#import "AudioPlayerPrompt.h"
#import <AVFoundation/AVFoundation.h>

@interface AudioPlayerPrompt()<AVAudioPlayerDelegate>
@property (strong ,nonatomic)AVAudioPlayer *wakeUpPlayer;
@property (strong ,nonatomic)AVAudioPlayer *upcomingPlayer;
@property (strong ,nonatomic)AVAudioPlayer *flowChartPlayer;
@property (strong ,nonatomic)AVAudioPlayer *addNodePlayer;
@property (strong ,nonatomic)AVAudioPlayer *deleteNodePlayer;
@property (strong ,nonatomic)AVAudioPlayer *emptyNodePlayer;
@property (copy) void (^actionBlock)(void);

@end

@implementation AudioPlayerPrompt

+ (AudioPlayerPrompt *)ShareAudioPlayerPrompt{
    static AudioPlayerPrompt *audioPlayerPrompt = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        audioPlayerPrompt = [[AudioPlayerPrompt alloc]init];
        [audioPlayerPrompt initWakeUpAudioPlayerPrompt];
        [audioPlayerPrompt initflowChartAudioPlayerPrompt];
        [audioPlayerPrompt initUpcomingAudioPlayerPrompt];
        [audioPlayerPrompt initaddNodeAudioPlayerPrompt];
        [audioPlayerPrompt initdeleteNodeAudioPlayerPrompt];
        [audioPlayerPrompt initemptyNodeAudioPlayerPrompt];
    });
    return audioPlayerPrompt;
}
-(void)initWakeUpAudioPlayerPrompt{
    NSString *musicNameString = [NSString stringWithFormat:@"weakUp"];
//    NSString *musicNameString = [NSString stringWithFormat:@"4"];
    NSString *path = [[NSBundle mainBundle] pathForResource:musicNameString ofType:@"m4a"];
//    NSString *path = [[NSBundle mainBundle] pathForResource:musicNameString ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    _wakeUpPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    _wakeUpPlayer.numberOfLoops = 0;
    _wakeUpPlayer.delegate = self;
    _wakeUpPlayer.volume = 0.8;
    [_wakeUpPlayer prepareToPlay];
    
}
-(void)initflowChartAudioPlayerPrompt{
    NSString *musicNameString = [NSString stringWithFormat:@"flowChart"];
    NSString *path = [[NSBundle mainBundle] pathForResource:musicNameString ofType:@"m4a"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    _flowChartPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    _flowChartPlayer.numberOfLoops = 0;
    _flowChartPlayer.delegate = self;
    _flowChartPlayer.volume = 0.8;
    [_flowChartPlayer prepareToPlay];
    
}
-(void)initUpcomingAudioPlayerPrompt{
    NSString *musicNameString = [NSString stringWithFormat:@"upcoming"];
    NSString *path = [[NSBundle mainBundle] pathForResource:musicNameString ofType:@"m4a"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    _upcomingPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    _upcomingPlayer.numberOfLoops = 0;
    _upcomingPlayer.delegate = self;
    _upcomingPlayer.volume = 0.8;
    [_upcomingPlayer prepareToPlay];
}
-(void)initaddNodeAudioPlayerPrompt{
    NSString *musicNameString = [NSString stringWithFormat:@"addNode"];
    NSString *path = [[NSBundle mainBundle] pathForResource:musicNameString ofType:@"m4a"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    _addNodePlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    _addNodePlayer.numberOfLoops = 0;
    _addNodePlayer.delegate = self;
    _addNodePlayer.volume = 0.8;
    [_addNodePlayer prepareToPlay];
}

-(void)initdeleteNodeAudioPlayerPrompt{
    NSString *musicNameString = [NSString stringWithFormat:@"deleteNode"];
    NSString *path = [[NSBundle mainBundle] pathForResource:musicNameString ofType:@"m4a"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    _deleteNodePlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    _deleteNodePlayer.numberOfLoops = 0;
    _deleteNodePlayer.delegate = self;
    _deleteNodePlayer.volume = 0.8;
    [_deleteNodePlayer prepareToPlay];
}
-(void)initemptyNodeAudioPlayerPrompt{
    NSString *musicNameString = [NSString stringWithFormat:@"emptyNode"];
    NSString *path = [[NSBundle mainBundle] pathForResource:musicNameString ofType:@"m4a"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error = nil;
    _emptyNodePlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    _emptyNodePlayer.numberOfLoops = 0;
    _emptyNodePlayer.delegate = self;
    _emptyNodePlayer.volume = 0.8;
    [_emptyNodePlayer prepareToPlay];
}

-(void)wakeUpPlay:(void (^)(void))actionBlock{
    [self stop];
    [_wakeUpPlayer play];
    _actionBlock = actionBlock;
}
-(void)upcomingPlay:(void (^)(void))actionBlock{
    [self stop];
    [_upcomingPlayer play];
    _actionBlock = actionBlock;
}
-(void)addNodePlay:(void (^)(void))actionBlock{
    [self stop];
    [_addNodePlayer play];
    _actionBlock = actionBlock;
}
-(void)deleteNodePlay:(void (^)(void))actionBlock{
    [self stop];
    [_deleteNodePlayer play];
    _actionBlock = actionBlock;
}
-(void)flowChartPlay:(void (^)(void))actionBlock{
    [self stop];
    [_flowChartPlayer play];
    _actionBlock = actionBlock;
}
-(void)emptyNodePlay:(void (^)(void))actionBlock{
    [self stop];
    [_emptyNodePlayer play];
    _actionBlock = actionBlock;
}
-(void)stop{
    [_wakeUpPlayer stop];
    [_upcomingPlayer stop];
    [_addNodePlayer stop];
    [_deleteNodePlayer stop];
    [_flowChartPlayer stop];
    [_emptyNodePlayer stop];
}
#pragma mark - AVAudioPlayerDelegate

// 音频播放完成时
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    // 音频播放完成时，调用该方法。
    // 参数flag：如果音频播放无法解码时，该参数为NO。
    //当音频被终端时，该方法不被调用。而会调用audioPlayerBeginInterruption方法
    // 和audioPlayerEndInterruption方法
    self.actionBlock();
    [player stop];
}

@end
