//
//
// Created by songjingjiu on 16-10-21.
//
//
#import
#import
"VoiceConverter.h"
@interface
VoicePlugin
:UIViewController<AVAudioRecorderDelegate,
AVAudioPlayerDelegate>
@property
(nonatomic,strong)
AVAudioRecorder
*audioRecorder;//音频录音机
@property
(nonatomic,strong)
AVAudioPlayer
*audioPlayer;//音频播放器,用于播放录音文件
@property
(nonatomic,strong)
AVAudioSession
*audioSession;
@end
@implementation
VoicePlugin
static
VoicePlugin*
voicePlugin = nil;
+(void)
unityMessage:(NSString
*)method
param:(NSString
*)msg
{
UnitySendMessage("PluginObj",[method
UTF8String],[msg
UTF8String]);
}
+(VoicePlugin
*)instance{
voicePlugin
=
[[VoicePlugin
alloc]
init];
[voicePlugin
setAudioSession];
return
voicePlugin;
}
-(void)setAudioSession{
if(_audioSession
==
nil){
_audioSession
=
[AVAudioSession
sharedInstance];
}
}
//NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
//获取Documents目录路径的方法:
NSString
*documentsDirectory=
nil;
NSString
*tempPath
= nil;
NSString
*destPath
= nil;
-(void)
startrecord:(NSString
*)path{
//对于多线程操作建议把线程操作放到@autoreleasepool中
@autoreleasepool
{
NSError
*sessionError
= nil;
[_audioSession
setCategory:AVAudioSessionCategoryPlayAndRecord
error:&sessionError];
[_audioSession
setActive:YES
error:&sessionError];
if
(sessionError)
{
NSLog(@"设置录音机对象时发生错误,错误信息:%@",sessionError.localizedDescription);
}
documentsDirectory=
[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
tempPath
=
[documentsDirectory
stringByAppendingPathComponent:@"/temp.wav"];
destPath
=
path;
//NSString
*tempCAFPath = [documentsDirectory
stringByAppendingPathComponent:@"/temp.caf"];
//NSString
*tempAIFPath = [documentsDirectory
stringByAppendingPathComponent:@"/temp.aif"];
tempPath
=
[documentsDirectory
stringByAppendingPathComponent:@"/temp.wav"];
NSMutableDictionary
*dicM=[NSMutableDictionary
dictionary];
//设置录音格式
[dicM setObject:@(kAudioFormatLinearPCM)
forKey:AVFormatIDKey];
//设置录音采样率,8000是电话采样率,对于一般录音已经够了
[dicM setObject:@(8000)
forKey:AVSampleRateKey];
//设置通道,这里采用单声道
[dicM setObject:@(1)
forKey:AVNumberOfChannelsKey];
//每个采样点位数,分为8、16、24、32
[dicM setObject:@(16)
forKey:AVLinearPCMBitDepthKey];
//是否使用浮点数采样
[dicM setObject:@(YES)
forKey:AVLinearPCMIsFloatKey];
NSError
*error
= nil;
_audioRecorder
=
[[AVAudioRecorder
alloc]initWithURL:[NSURL
URLWithString:tempPath]
settings:dicM
error:&error];_audioRecorder.delegate
=
self;
_audioRecorder.meteringEnabled
=
YES;//如果要监控声波则必须设置为YES
if([_audioRecorder
isRecording]){
[_audioRecorder
stop];
}
if([_audioRecorder
prepareToRecord]){
[_audioRecorder
record];
}
if
(error)
{
[VoicePlugin
unityMessage:@"RecordCallBack"
param:error.localizedDescription];
NSLog(@"创建录音机对象时发生错误,错误信息:%@",error.localizedDescription);
}
NSLog(@"startrecord finalPath :%@ size: %ld" ,
tempPath,
[voicePlugin
getFileSize:tempPath]);
}
}
-(void)
stoprecord{
@autoreleasepool
{
if(_audioRecorder
!=
nil){
[_audioRecorder
stop];
}
if
([VoiceConverter
ConvertWavToAmr:tempPath
amrSavePath:destPath]){
NSLog(@"wav转amr成功");
[VoicePlugin
unityMessage:@"RecordCallBack"
param:@""];
}else{
NSLog(@"wav转amr失败");
[VoicePlugin
unityMessage:@"RecordCallBack"
param:@"wav转amr失败"];
}
NSLog(@"startrecord destPath :%@ size: %ld" ,
destPath,
[voicePlugin
getFileSize:destPath]);
}
}
-(void)
startplay:(NSString
*)path{
@autoreleasepool
{
NSError
*sessionError
= nil;
[_audioSession
setCategory
:AVAudioSessionCategoryPlayback
error:&sessionError];
[_audioSession
setActive:YES
error:&sessionError];
if
(sessionError)
{
NSLog(@"设置录音机对象时发生错误,错误信息:%@",sessionError.localizedDescription);
}
NSLog(@"startplay path :%@ " ,
path);
NSError
*error
= nil;
//NSString
*documentsDirectory= [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString
*tempTransferPath =
[documentsDirectory
stringByAppendingPathComponent:@"/tempTransferPath.wav"];
if
([VoiceConverter
ConvertAmrToWav:path
wavSavePath:tempTransferPath]){
NSLog(@"amr转wav成功");
}else{
NSLog(@"amr转wav失败");
[VoicePlugin
unityMessage:@"PlayCallBack"
param:@"amr转wav失败"];
}
NSLog(@"startplay tempTransferPath :%@ size: %ld"
,
tempTransferPath, [voicePlugin
getFileSize:tempTransferPath]);
_audioPlayer
=
[[AVAudioPlayer
alloc]initWithContentsOfURL:[NSURL
URLWithString:tempTransferPath]
error:
&error];
_audioPlayer.numberOfLoops
=
0;
_audioPlayer.delegate
=
self;
[_audioPlayer
prepareToPlay];
if([_audioPlayer
isPlaying]){
[_audioPlayer
stop];
}
[_audioPlayer
play];
if
(error)
{
[VoicePlugin
unityMessage:@"PlayCallBack"
param:error.localizedDescription];
NSLog(@"播放录音机对象时发生错误,错误信息:%@",error.localizedDescription);
}else{
[VoicePlugin
unityMessage:@"PlayCallBack"
param:@""];
}
}
}
-(void)
stopplay{
@autoreleasepool
{
if(_audioPlayer
!=
nil){
[_audioPlayer
stop];
}
}
}
#pragma
mark - 获取文件大小
-
(NSInteger)
getFileSize:(NSString*)
path{
NSFileManager
*
filemanager = [[NSFileManager
alloc]init];
if([filemanager
fileExistsAtPath:path]){
NSDictionary
*
attributes = [filemanager attributesOfItemAtPath:path
error:nil];
NSNumber
*theFileSize;
if
(
(theFileSize = [attributes objectForKey:NSFileSize])
)
return[theFileSize intValue];
else
return
-1;
}
else{
return
-1;
}
}
-(void)dealloc
{
}
@end
#ifdef
_cplusplus
extern
"C"{
#endif
extern
void
UnitySendMessage(const
char*,
const
char*,
const
char*);
extern
NSString*
_CreateNSString(const
char*
string);
NSString*
_CreateNSString(const
char*
string)
{
if(string){
return
[NSString
stringWithUTF8String:string];
}else{
return
[NSString
stringWithUTF8String:""];
}
}
VoicePlugin
*voice
= NULL;
void
_StartRecord(const
char
*path) {
NSLog(@"_StartRecord");
if(voice
==
NULL){
voice
=
[VoicePlugin
instance];
}
NSLog(@"voice");
//[voice
startrecord:[[NSString alloc] initWithCString:path]];
[voice
startrecord:_CreateNSString(path)];
}
void
_StopRecord()
{
NSLog(@"_StopRecord");
if(voice
==
NULL){
voice
=
[VoicePlugin
instance];
}
[voice
stoprecord];
}
void
_StartPlay(const
char
*path){
NSLog(@"_StartPlay");
if(voice
==
NULL){
voice
=
[VoicePlugin
instance];
}
NSLog(@"voice");
//[voice
startplay:[[NSString alloc] initWithCString:path]];
[voice
startplay:_CreateNSString(path)];
}
void
_StopPlay(){
NSLog(@"_StopPlay");
if(voice
==
NULL){
voice
=
[VoicePlugin
instance];
}
[voice
stopplay];
}
#ifdef
_cplusplus
}
#endif