关于使用 AVAudioPlayer 自动录制和自动播放 AVAudioRecorder
Posted
技术标签:
【中文标题】关于使用 AVAudioPlayer 自动录制和自动播放 AVAudioRecorder【英文标题】:About auto record and auto play with AVAudioPlayer&AVAudioRecorder 【发布时间】:2012-06-16 02:10:03 【问题描述】:在 .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;
这里有一些问题, first:audioPlayerDidFinishPlaying这个函数根本没有做,这段代码只能运行“开始播放”,但实际上声音并没有播放,我以前用录音机在录音状态下检查音量,但是 peakPowerForChannel:0 只返回一个静态值 0.00001,我不知道如何解决这个问题,任何人都可以帮助我或给我发送自动录制和自动播放的示例代码?对此,我真的非常感激。谢谢
【问题讨论】:
我认为没有调用 audioPlayerDidFinishPlaying 方法,因为您将 player.delegate = self; 设置在非常奇怪的地方。最好在播放器初始化后立即设置委托。 【参考方案1】:是的,已经 5 年过去了,我仍然与 iOS 开发人员合作,这个问题在提出这个问题后大约一个月内得到解决,我不记得看到这个答案。我当时做了一个像会说话的应用程序。而且已经完成了,但是对我来说没用。
【讨论】:
以上是关于关于使用 AVAudioPlayer 自动录制和自动播放 AVAudioRecorder的主要内容,如果未能解决你的问题,请参考以下文章
录制音频并保存到文档目录后无法使用 AVAudioPlayer 播放音频文件
iPhone SDK:使用核心音频 AVAudioPlayer 更改播放速度
Swift AVAudioPlayer 不会播放用 AVAudioRecorder 录制的音频