iOS录音和播放

作者: FlyElephant | 来源:发表于2016-06-06 14:51 被阅读6201次

现在的社交应用中录音和播放时必不可少的一部分,当然有些为了互动性更强也会加入一些声音,关于录音的博客网上也是不胜其数,当然效果也是差强人意,要么是到处转载,要么就是下载之后发现不能录音或者不能播放,如果你想做一些些简单的录音和播放,可以参考本文,先来看一下效果图:

FlyElephant.png

录音

AVAudioRecorder是录音必须要用的,其次就是要设置录音的路径,存放在本地,之后的播放或者说上传都通过此路劲进行进行操作,简单看一下核心代码:
<pre><code>`

NSLog(@"FlyElephant-startRecording");;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

NSError *error = nil;
self.audioRecorder = [[ AVAudioRecorder alloc] initWithURL:self.recordUrl settings:[self audioRecordingSettings] error:&error];
self.audioRecorder.meteringEnabled = YES;
if ([self.audioRecorder prepareToRecord] == YES){
    self.audioRecorder.meteringEnabled = YES;
    [self.audioRecorder record];
}else {
    NSLog(@"FlyElephant--初始化录音失败");
}`</code></pre>

录音路径:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(
                                                        NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir
                           stringByAppendingPathComponent:@"recordTest.caf"];

NSURL *url = [NSURL fileURLWithPath:soundFilePath];
self.recordUrl=url;

录音设置:
<pre><code>`

  • (NSDictionary *)audioRecordingSettings{

    NSDictionary *result = nil;

    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
    //设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
    // [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
    //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
    //录音通道数 1 或 2
    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
    //线性采样位数 8、16、24、32
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    //录音的质量
    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

    result = [NSDictionary dictionaryWithDictionary:recordSetting];
    return result;
    }`</code></pre>

录音播放

AVAudioPlayer是必不可少的,调用起来非常方便,设置循环次数,直接播放即可,当然也可以进行声音设置,根据实际需要个人设置即可:

NSLog(@"FlyElephant-playRecording");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recordUrl error:&error];
self.audioPlayer.numberOfLoops = 0;
[self.audioPlayer play];
NSLog(@"FlyElephant-playing");

声音没法展示,看一下Log:
<pre><code>**2016-06-06 14:49:56.304 FEAudioRecord[2341:1257382] FlyElephant-startRecording** **2016-06-06 14:49:59.838 FEAudioRecord[2341:1257382] stopRecording** **2016-06-06 14:49:59.873 FEAudioRecord[2341:1257382] stopped** **2016-06-06 14:50:01.239 FEAudioRecord[2341:1257382] FlyElephant-playRecor**</code></pre>

相关文章

网友评论

  • Sunshine_an:老铁 demo有吗 发邮箱1437050181@qq.com 谢谢你
    Sunshine_an:@bbLin 看到邮箱了。。。我发你
    Sunshine_an:@bbLin 怎么发你啊
    bbLin:你好,请问你找到demo了吗,能不能发我840701546@qq.com 感谢感谢

本文标题:iOS录音和播放

本文链接:https://www.haomeiwen.com/subject/ofzrdttx.html