2

在 .h 文件中我这样写:

#import <UIKit/UIKit.h>
#import "SCListener.h"
#import <AVFoundation/AVFoundation.h>

@interface TalkingAndSayingViewController : UIViewController<AVAudioPlayerDelegate>
{

IBOutlet UIImageView *bkg;
IBOutlet UILabel *showVoulme;

SCListener *listener;

NSTimer *time;

BOOL listen;
NSString *audioPath;
AVAudioRecorder *recoder;
AVAudioPlayer *player;

int breakCount;

BOOL timeActive;

}
@property(nonatomic,assign)int roleIndex;
@end

在 .m 文件中我写了这个:

- (void)viewDidLoad
{

[super viewDidLoad];

NSArray *searchPaths =  NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
audioPath = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"audio.wav"];
[audioPath retain];

listen = YES;

NSMutableDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                                       [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                                       [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                                       [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                                       nil];

NSString *imgName = [NSString stringWithFormat:@"role%d.png",self.roleIndex];
[bkg setImage:[UIImage imageNamed:imgName]];

recoder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioPath] settings:recordSettings error:nil];
[recoder setMeteringEnabled:YES];

player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:nil];
[player setNumberOfLoops:0];

AVAudioSession *audioSession = [[AVAudioSession sharedInstance] retain];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive:YES error: nil];

time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(CheckVolume) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];

}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
     timeActive = YES;
        listen = YES;
        NSLog(@"fihish playing, start listen");
}

-(void)CheckVolume

    {    
        Float32 peak = [[SCListener sharedListener] peakPower];
        if(timeActive)
        {
            if(listen)
            {
                [showVoulme setText:[NSString stringWithFormat:@"%f",peak]];
                if(peak>0.3)
                {
                    listen = NO;
                    [recoder prepareToRecord];
                    [recoder record];
                    NSLog(@"start recording");
                }
            }

            else
            {
                if(peak<0.1)
                {
                    if(breakCount >= 5)
                    {
                        NSLog(@"start play");

                        [recoder stop];

                        player.delegate = self;
                        [player prepareToPlay];
                        [player play];
                        timeActive = NO;
                        breakCount = 0;
                    }
                    else 
                        breakCount++;
                }
                else
                    breakCount = 0;
            }
        }
    }

-(void)viewWillDisappear:(BOOL)animated
{
    [recoder stop];
    [[SCListener sharedListener] stop];

    timeActive = NO;
}

-(void)viewDidAppear:(BOOL)animated
{
    timeActive = YES;

    [[SCListener sharedListener] listen];

    breakCount = 0;
}

这里有一些问题,首先:audioPlayerDidFinishPlaying这个函数根本没有做,而且这段代码只能运行“开始播放”,但声音实际上并没有播放,我以前用录音机检查音量的时候处于录制状态,但是 peakPowerForChannel:0 只返回一个静态值 0.00001,我不知道如何解决这个问题,谁能帮我或发个自动录制和自动播放的示例代码?对此,我真的非常感激。谢谢

4

1 回答 1

0

是的,已经 5 年过去了,我仍然与 iOS 开发人员合作,这个问题在提出这个问题后大约一个月内得到解决,我不记得看到这个答案。我当时做了一个像会说话的应用程序。并且已经完成了,然而,对我来说没用。

于 2017-12-07T13:23:08.460 回答