iOS实现录音与播放功能

本文介绍了如何在iOS中实现录音和播放功能。通过使用AVFoundation框架,设置录音设置,创建AVAudioRecorder实例进行录音,并使用AVAudioPlayer播放录音。此外,还展示了如何显示波形图以及处理录音时间戳。
摘要由CSDN通过智能技术生成

视同控制器.h


//

//  ACYL_RecordViewController.h

//  Ancun

//

//  Created by Dubai on 15/4/1.

//

//


#import <UIKit/UIKit.h>


#import <AudioToolbox/AudioToolbox.h>

#import <AVFoundation/AVFoundation.h>



@interface ACYL_RecordViewController : UIViewController<AVAudioRecorderDelegate>


{

    AVAudioRecorder *recorder;

    NSTimer *timer;

    NSURL *urlPlay;

}

@property (strong, nonatomic) UIButton *recordBtn;//录音按钮

@property (strong, nonatomic) UIButton *playBtn;//播放按钮

@property (strong ,nonatomic) UIImageView *imageViewWave;//显示波形图的视图

@property (strong, nonatomic) AVAudioPlayer *avPlay;



@end


视图控制器.m


//

//  ACYL_RecordViewController.m

//  Ancun

//

//  Created by Dubai on 15/4/1.

//

//


#import "ACYL_RecordViewController.h"


#define SCREEN_WIDTH    ([UIScreen mainScreen].bounds.size.width)

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@interface ACYL_RecordViewController ()

{



    NSTimer *timeRecordNow;//录音时间戳

    UILabel *timeLabel;//显示时间戳


}

@end


@implementation ACYL_RecordViewController


-(void)viewWillAppear:(BOOL)animated

{

    self.view.backgroundColor = [UIColor whiteColor];

    self.tabBarController.tabBar.translucent = NO;

}


-(void)viewDidLoad

{

    [super viewDidLoad];

    

    

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    [audioSession setActive:YES error:nil];

    if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {

        [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {

            if (granted) {

                // Microphone enabled code

            }

            else {

                // Microphone disabled code

            }

        }];

    }

    [recorder prepareToRecord];

    recorder.meteringEnabled = YES;

    [recorder record];


    

    [self audio];//设置录音

    

    //波形图

    self.imageViewWave = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -SCREEN_WIDTH/1.2, 80.0, SCREEN_WIDTH/1.5, 200)];

    self.imageViewWave.backgroundColor = [UIColor redColor];

    [self.view addSubview:self.imageViewWave];

    self.recordBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];

    self.recordBtn.frame = CGRectMake(SCREEN_WIDTH/6, self.imageViewWave.frame.origin.y+self.imageViewWave.frame.size.height+30, 60, 40);

    [self.recordBtn setTitle:@"录音"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值