码迷,mamicode.com
首页 > 微信 > 详细

类似微信发送语音,按下录音,松开结束录音;并且可播放

时间:2016-11-03 16:15:39      阅读:785      评论:0      收藏:0      [点我收藏+]

标签:layer   nbsp   cat   ret   ack   content   dex   tor   uidevice   

需要导入

#import <AVFoundation/AVFoundation.h>

利用此框架中的

AVAudioRecorderAVAudioPlayer来录音和播放以下是AVAudioRecorder录音的使用方法:

 

- (IBAction)downAction:(id)sender {

    按下录音

    if ([self canRecord]) {

        

        NSError *error = nil;

        必须真机上测试,模拟器上可能会崩溃

        recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:playName] settings:recorderSettingsDict error:&error];

        

        if (recorder) {

            是否允许刷新电平表,默认是off

            recorder.meteringEnabled = YES;

            创建文件,并准备录音

            [recorder prepareToRecord];

            开始录音

            [recorder record];

            

            启动定时器,为了更新电平

            timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES];

            

        } else

        {

            int errorCode = CFSwapInt32HostToBig ([error code]);

            NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);

            

        }

    }

    

}

 

- (IBAction)upAction:(id)sender {

    松开 结束录音

    

    录音停止

    [recorder stop];

    recorder = nil;

    结束定时器

    [timer invalidate];

    timer = nil;

    图片重置

    soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:0]];

    

}

[cpp] view plain copy

 

- (IBAction)downAction:(id)sender {

    按下录音

    if ([self canRecord]) {

        

        NSError *error = nil;

        必须真机上测试,模拟器上可能会崩溃

        recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:playName] settings:recorderSettingsDict error:&error];

        

        if (recorder) {

            是否允许刷新电平表,默认是off

            recorder.meteringEnabled = YES;

            创建文件,并准备录音

            [recorder prepareToRecord];

            开始录音

            [recorder record];

            

            启动定时器,为了更新电平

            timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(levelTimer:) userInfo:nil repeats:YES];

            

        } else

        {

            int errorCode = CFSwapInt32HostToBig ([error code]);

            NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);

            

        }

    }

    

}

 

 

- (IBAction)upAction:(id)sender {

    松开 结束录音

    

    录音停止

    [recorder stop];

    recorder = nil;

    结束定时器

    [timer invalidate];

    timer = nil;

    图片重置

    soundLodingImageView.image = [UIImage imageNamed:[volumImages objectAtIndex:0]];

    

}

 

以下是AVAudioPlayer播放器的使用方法:

- (IBAction)playAction:(id)sender {

    

    NSError *playerError;

    

    播放

    player = nil;

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:playName] error:&playerError];

    

    if (player == nil)

    {

        NSLog(@"ERror creating player: %@", [playerError description]);

    }else{

        [player play];

    }

    

}

 

 

- (IBAction)playAction:(id)sender {

    

    NSError *playerError;

    

    播放

    player = nil;

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:playName] error:&playerError];

    

    if (player == nil)

    {

        NSLog(@"ERror creating player: %@", [playerError description]);

    }else{

        [player play];

    }

    

}

 

提示:如果是7.0第一次运行会提示,是否允许使用麦克风

 

if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)

{

    7.0第一次运行会提示,是否允许使用麦克风

    AVAudioSession *session = [AVAudioSession sharedInstance];

    NSError *sessionError;

    AVAudioSessionCategoryPlayAndRecord用于录音和播放

    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

    if(session == nil)

        NSLog(@"Error creating session: %@", [sessionError description]);

        else

            [session setActive:YES error:nil];

}

 

if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)

{

    7.0第一次运行会提示,是否允许使用麦克风

    AVAudioSession *session = [AVAudioSession sharedInstance];

    NSError *sessionError;

    AVAudioSessionCategoryPlayAndRecord用于录音和播放

    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

    if(session == nil)

        NSLog(@"Error creating session: %@", [sessionError description]);

        else

            [session setActive:YES error:nil];

}

 

 

 

类似微信发送语音,按下录音,松开结束录音;并且可播放

标签:layer   nbsp   cat   ret   ack   content   dex   tor   uidevice   

原文地址:http://www.cnblogs.com/whutDaniel/p/6026801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!