尝试用iPhone的麦克风录制声音,并用扬声器播放(插入耳机时耳机会播放)。
您目前正在使用此设置和音频播放.可以通过前置摄像头附近的耳机播放,而不是通过手机底部的(大的)扬声器播放:
ns string * tempdir=nstemporarydirectory (;
ns string * sound file path=[ tempdirstringbyappendingpathcomponent : @ ' sound.caf ' ];
ns URL * sound fileurl=[ nsurlfileurlwithpath : sound file path ];
ns dictionary * record settings=@ { avencoderaudioqualitykey : [ nsnumbernumberwithint 3360 avaudioqualitymin ],
AVEncoderBitRateKey: @16,
AVNumberOfChannelsKey: @2、
AVSampleRateKey: @44100.0};
NSError *error=nil;
avaudiosession * audiosession=[ avaudiosessionsharedinstance ];
[ audiosessionsetcategory : avaudiosessioncategoryplayandrecorderror : nil ]
_ audio recorder=[ [ avaudiorecorderalloc ] initwithurl 3360 soundfileurlsettings 3360 recordsettingserror 3360 error ];
我尝试过这样的事情: (更改了文件扩展名,将AVFormatIDKey添加到了设置中。
ns string * sound file path=[ tempdirstringbyappendingpathcomponent : @ ' sound.AAC ' ];
ns URL * sound fileurl=[ nsurlfileurlwithpath : sound file path ];
//Set settings for audio recording
ns dictionary * record settings=@ { avformatidkey : [ nsnumbernumberwithint 3360 kaudioformatmpeg4AAC ],
avencoderaudioqualitykey : [ nsnumbernumberwithint : avaudioqualitymin ]、
AVEncoderBitRateKey: @16,
AVNumberOfChannelsKey: @2、
AVSampleRateKey: @44100.0};
但后来我发现
error : theoperationcouldn’tbe completed.(osstatuserror 2003334207.) )。
将文件扩展名更改为. caf不会报告错误,但不会再生任何内容。
这是播放代码:
NSError *error;
_ audio player=[ avaudioplayeralloc ] initwithcontentsofurl 3360 _ audio recorder.URL error 3360 error ]
_audioPlayer.delegate=self;
if(error ) nslog ) @'error:%@ ',[error localizedDescription];
else [_audioPlayer play];
简单来说,在使用iPhone扬声器或插入耳机时,如何录制和播放aac文件?
----------- -编辑-------编辑
使用此设置保存到. aac文件。
ns dictionary * record settings=@ { avformatidkey : @ (kaudioformatmpeg4AAC ),
avencoderaudioqualitykey : @ (avaudioqualitymin )、
AVSampleRateKey: @16000.0、
AVNumberOfChannelsKey: @1};
只需通过扬声器播放,用户就能实际听到录制的内容