如何在单击下一个按钮时终止音频

时间:2018-04-27 07:18:23

标签: ios objective-c avspeechsynthesizer

@interface SpeechCommonFunctions : NSObject<AVAudioRecorderDelegate,AVAudioPlayerDelegate>
{
    AVAudioRecorder *recorder;

}
@property(strong,nonatomic)AVAudioPlayer *player;

@property(assign)BOOL isRecordComplete;

-(void)textToSpeechAction:(NSMutableArray *)imageStoreArray :(int)counter :(UIImageView *)imageChangeImageView :(UIImageView *)spekerOrMic :(BOOL)isMicPresent;

-(void)recordPlayAction;

@property(assign)BOOL isWritePresent;

speechclass: -

@implementation SpeechCommonFunctions

-(void)textToSpeechAction:(NSMutableArray *)imageStoreArray :(int)counter :(UIImageView *)imageChangeImageView :(UIImageView *)spekerOrMic :(BOOL)isMicPresent
{
  //  spekerOrMic.image = [UIImage imageNamed:@"speaker.png"];
    NSArray *items = [[imageStoreArray objectAtIndex:counter] componentsSeparatedByString:@"."];
    NSString *speechString;
    if(_isWritePresent)
    {
       NSArray *viewToRemove = [spekerOrMic subviews];
        for (UIImageView *v in viewToRemove) {
          [v removeFromSuperview];
      }
      spekerOrMic.image = [UIImage imageNamed:@""];
        spekerOrMic.backgroundColor = [UIColor colorWithRed:41/255.0 green:52/255.0 blue:44/255.0 alpha:1.0];
        NSString *tempString = [items objectAtIndex:0];
        NSArray *tempArray = [tempString componentsSeparatedByString:@" "];
        speechString = [tempArray objectAtIndex:1];
    }
    else
    {
        speechString = [items objectAtIndex:0];
    }

    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];

    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:speechString];



    [utterance setRate: 0.23f ];
   // utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
     utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
    [synthesizer speakUtterance:utterance];

    imageChangeImageView.image = [UIImage imageNamed:[imageStoreArray objectAtIndex:counter]];


    //if the record there then only use it....



    if(isMicPresent)
  {
     [NSTimer scheduledTimerWithTimeInterval:NO target:self selector:@selector(micAction:) userInfo:spekerOrMic repeats:NO];
   }
}

-(void)micAction:(NSTimer *)timer
{
    NSLog(@"mic action");
    UIImageView *micOrSpeaker = timer.userInfo ;
    micOrSpeaker.image = [UIImage imageNamed:@"mic.png"];
   // Set the audio file
  NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"MyAudioMemo.m4a",
                               nil];
   NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

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

    // Define the recorder setting
    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];

    [recorder record];

    [NSTimer scheduledTimerWithTimeInterval:NO target:self selector:@selector(recordStopAction:) userInfo:micOrSpeaker repeats:NO];
}

-(void)recordStopAction:(NSTimer *)timer
{
    NSLog(@"stop");
    [recorder stop];
    UIImageView *micOrSpeaker = timer.userInfo;
    micOrSpeaker.image = [UIImage imageNamed:@""];

    _isRecordComplete = YES;

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:NO error:nil];
}

-(void)recordPlayAction
{
    if (!recorder.recording){
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
        [_player setDelegate:self];
        [_player play];
    }
}

@end

我在班上调用了这个函数,如下所示......

        [commonFunctionObject textToSpeechAction:numbersStoreArray :counter :_numbersShowImageView :_speakerOrMicImageView :isMicPresent];
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(ActionToCkeckRecordCompletion) userInfo:nil repeats:YES];
-(void)ActionToCkeckRecordCompletion
{
    if(commonFunctionObject.isRecordComplete)
    {
        _confirmationPopupView.hidden = false;
    }
}
- (IBAction)nextButtonAction:(id)sender
{
    counter+=1;
    _confirmationPopupView.hidden = true;
    commonFunctionObject.isRecordComplete = NO;
    if(commonFunctionObject.player.playing){[commonFunctionObject.player stop];}
    [self repeatActionFire];
}

- (IBAction)retryButtonAction:(id)sender
{

    counter+=-1;
    if(counter==-1){
        [self viewDidLoad];
    }
    else{
        //  _confirmationPopupView.hidden = true;
        commonFunctionObject.isRecordComplete = NO;
        if(commonFunctionObject.player.playing){[commonFunctionObject.player stop];}

        [self repeatActionFire];
    }

}

数组为: -

 numbersStoreArray = [[NSMutableArray alloc]initWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png", nil];

这里我得到了输出...... 但是当我快速点击下一个按钮然后图像正在改变,但是音频没有来。所以该怎么做。我想在这里有一些不需要的代码。所以请给出解决方案。

0 个答案:

没有答案