在ios中编写程序实现录音功能

本博客介绍了一个实现录音播放控制与录制状态管理的iOS应用实例,详细解释了如何通过按钮实现开始、停止和播放录音的功能,并在不同操作状态间进行切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <UIKit/UIKit.h>
#import "AVFoundation/AVFoundation.h"
@interface AudioRecoderViewController : UIViewController
{
    UILabel *label;
    AVAudioRecorder *recorder;    //定义专门录制的类AVAudioRecoder
   
    AVAudioPlayer *player;
}
@property(nonatomic,retain) AVAudioRecorder *recorder;
@property(nonatomic,retain) AVAudioPlayer *player;
@end

 

#import "AudioRecoderViewController.h"

@interface AudioRecoderViewController ()

@end

@implementation AudioRecoderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)loadView{
    //定义UIView
    UIView *view=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame] ;
    view.backgroundColor=[UIColor purpleColor];
    self.view=view;
//初始化label
    label=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 160, 40)];
    label.text=@"等待录制";
    label.textColor=[UIColor greenColor];
    label.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:label];
   
    //定义按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(90, 100, 160, 40);
    [button setTitle:@"开始录制" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(startRecoder) forControlEvents:UIControlEventTouchUpInside];
    //添加显示
    [self.view addSubview:button];
   
    //定义按钮
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(90, 160, 160, 40);
    [btn2 setTitle:@"停止录制" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(stopRecoder) forControlEvents:UIControlEventTouchUpInside];
    //添加显示
    [self.view addSubview:btn2];
   
    //定义按钮
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn3.frame = CGRectMake(90, 220, 160, 40);
    [btn3 setTitle:@"播放录制" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(startRecoderPlay) forControlEvents:UIControlEventTouchUpInside];
   
    //添加显示
    [self.view addSubview:btn3];
   
}
-(void)startRecoder{
    //设置label的显示 显示为正在录制
    label.textColor=[UIColor redColor];
    label.text=@"录制中...";
    label.textAlignment=NSTextAlignmentCenter;
   
    //判断当前的录制状态和播放状态
    if (recorder.isRecording)
    {
        [recorder stop];
    }
    if (player.isPlaying)
    {
        [recorder stop];
    }
    NSError
}

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值