ios语音通讯解决方案 wav转amr

最近在倒腾语音,遇到一些问题,再次记录一下,方便日后使用。

        0.ios录制。

1.ios录制语音转化为 amr格式,至于为什么用amr,就不在此赘述了。

2.在静音状态下播放语音。

3.播放过后,整体音量变小。

4.听筒播放和喇叭播放切换。

1.ios录制

使用AVAudioRecorder, 即可完成录音操作。

        NSString* pcm_path = @”路径“;
        _recordedTmpFile = [[NSURL alloc] initFileURLWithPath:pcm_path];
        //LinearPCM 是iOS的一种无损编码格式,但是体积较为庞大
        //录音设置
        NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
        //录音格式 无法使用
        [recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
        //采样率
        [recordSettings setValue :[NSNumber numberWithFloat:8000.0] forKey: AVSampleRateKey];//44100.0
        //通道数
        [recordSettings setValue :[NSNumber numberWithInt:1] forKey: AVNumberOfChannelsKey];
        //线性采样位数
        //[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
        //音频质量,采样质量
        [recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
        
        //Setup the recorder to use this file and record to it.
        _recorder = [[ AVAudioRecorder alloc] initWithURL:_recordedTmpFile settings:recordSettings error:nil];
        _recorder.delegate = self;
        _recorderTime = 0.0f;
    [_recorder prepareToRecord];
    [_recorder record];


2.格式转换。

此处主要使用ibopencore-amr库(Go)。下载下来进行编译,编译脚本网上也可以找到。此处提供懒人模式,密码: 89py。
文件编译好过后,正常导入使用即可。
在使用过成功遇到的一个问题就是。安卓录制的是非amr文件,导致转码失败。此时在读取需要转码文件的时候,看看文件的头部。是不是“

#!AMR\n”。

使用方法:直接调用amrFileCodec.h下面的

EncodeWAVEFileToAMRFile,  

DecodeAMRFileToWAVEFile就可以进行转码了。

3.用户静音状态下播放

//不会停止其他程序播放,当设备设置为静音时,无法播放。锁屏会停止播放
NSString * const AVAudioSessionCategoryAmbient = @"AVAudioSessionCategoryAmbient";
//停止其他程序播放,当设备设置为静音时,
NSString *const AVAudioSessionCategorySoloAmbient = @"AVAudioSessionCategorySoloAmbient";
//停止播放其他程序音频,设备设置为静音的时候,音频依然会继续播放。
NSString *const AVAudioSessionCategoryPlayback = @"AVAudioSessionCategoryPlayback";
//只能进行录音
NSString *const AVAudioSessionCategoryRecord = @"AVAudioSessionCategoryRecord";
//同时进行录音和播放
NSString *const AVAudioSessionCategoryPlayAndRecord = @"AVAudioSessionCategoryPlayAndRecord";
//音频处理,不能进行录制和播放
NSString *const AVAudioSessionCategoryAudioProcessing = @"AVAudioSessionCategoryAudioProcessing";
    AVAudioSession * audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];

可以根据实际情况,选择合适自己的。

 
 

4.音量

此处需要设置一下,可能会出现音量变小的问题。
	AVAudioSession * audioSession = [AVAudioSession sharedInstance];
    [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];


5.听筒和喇叭的切换

需要监听听筒旁边的设备,当设备被挡住时,也就是在接听电话状态。

[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(senderStateChange:)
 name:@"UIDeviceProximityStateDidChangeNotification" 
error:nil];

- (void)senderStateChange:(NSNotificationCenter*)not
{
    if ([[UIDevice currentDevice] proximityState] == YES)
    {
        NSLog(@"Device is close to user");
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        
    }
    else
    {
        NSLog(@"Device is not close to user");
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        
    }
}



后记:
下载中有.cpp文件,可以把文件改成点.m。都是c语言的语法。或者是把引用该文件的文件名改成.mm。
最后是需要把自己的语音上传后台,然后再下载下来进行播放即可。
为了避免广告嫌疑,自己找找。有第三方平台,可以上传第三方,然后在下载下来播放。这样自己的后台工作量就简单多了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值