您现在的位置是:首页 > 数码 > 

ios录音小操作

2025-07-20 21:18:30
ios录音小操作 // ViewController.h // 录音#import <UIKit/UIKit.h>@interface ViewController : UIViewController - (IBAction)clickRecordBtn:(id)sender; - (IBAction)clickPauseBtn:(id)sender; - (IBAct

ios录音小操作

//  ViewController.h
//  录音#import <UIKit/UIKit.h>@interface ViewController : UIViewController
- (IBAction)clickRecordBtn:(id)sender;
- (IBAction)clickPauseBtn:(id)sender;
- (IBAction)clickResumeBtn:(id)sender;
- (IBAction)clickStopBtn:(id)sender;@property (weak, nonatomic) IBOutlet UIProgressView *progressView;@end//  
//  录音#import ViewController.h
#import <AVFoundation/AVFoundation.h>@interface ViewController ()<AVAudioPlayerDelegate,AVAudioRecorderDelegate>@property (nonatomic, strong) AVAudioPlayer *audioPlay; //音频播放器
@property (nonatomic, strong) AVAudioRecorder *audioRecored; //音频录音机@property (nonatomic, strong) STimer *timer;@end@implementation ViewController-(STimer *)timer
{if (!_timer) {_timer = [STimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(aduioPowerChange) userInfo:nil repeats:YES];}return _timer;
}#pragma mark 录音声波的状态监测
-(void)aduioPowerChange
{[self.audioRecored updateMeters];//更新测量值float power= [self.audioRecored averagePowerForChannel:0];//取得第一个通道的音频,注意音频(声音)强度范围时-160到0 ,0代表最大输入SLog(@.......... %f,power);CGFloat progress=(1.0/50.0)*(power50.0);[_progressView setProgress:progress];
}- (void)viewDidLoad
{[super viewDidLoad];//设置音频会话[self setAudioSession];
}#pragma mark 设置音频会话
-(void)setAudioSession
{AVAudioSession *audioSession = [AVAudioSession sharedInstance];//设置为播放和录音的状态,以便可以在录制完成后播放[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];[audioSession setActive:YES error:nil];
}#pragma mark 获得音频的URL
-(SURL *)getAudioUrl
{SString *documentPath = [SSearchPathForDirectoriesInDomains(SDocumentDirectory, SUserDomainMask, YES) lastObject];SString *audioPath = [documentPath stringByAppendingPathComponent:@test.wav];SURL *url = [SURL fileURLWithPath:audioPath];return url;
}#pragma mark 获得音频播放器
-(AVAudioPlayer *)audioPlay
{if (!_audioPlay) {//1. 获得url路径SURL *audioUrl = [self getAudioUrl];SError *error;_audioPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:audioUrl error:&error];_audioPlay.volume = 1.0;if (error) {SLog(@创建音频播放器的时候出现了错误!%@,[error localizedDescription]);}}return _audioPlay;
}#pragma mark 获得录音机对象
-(AVAudioRecorder *)audioRecored
{if (!_audioRecored){//1. 创建录音后的音频的保存路径SURL *audioUrl = [self getAudioUrl];//2. 录音机的文件设置SMutableDictionary *dic = [SMutableDictionary dictionary];//设置录音的音频格式[dic setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];//设置录音的采样率 电话采样率为8000[dic setObject:@(8000) forKey:AVSampleRateKey];//设置通道[dic setObject:@(1) forKey:AVumberOfChannelsKey];//采样点位数 8、16、24、2[dic setObject:@(8) forKey:AVLinearPCMBitDepthKey];//是否采用浮点数采样[dic setObject:@(YES) forKey:AVLinearPCMIsFloatKey];//其他设置......//. 创建录音机SError *error;_audioRecored = [[AVAudioRecorder alloc] initWithURL:audioUrl settings:dic error:&error];if (error) {SLog(@创建录音机失败);}_=YES;//如果要监控声波则必须设置为YES_audioRecored.delegate = self;}return _audioRecored;
}#pragma mark 点击录音按钮
- (IBAction)clickRecordBtn:(id)sender {if (!self.audioRecored.isRecording) {[_audioPlay stop];[_audioRecored record];fireDate = [SDate distantPast];}
}#pragma mark 点击暂停按钮
- (IBAction)clickPauseBtn:(id)sender {if (self.audioRecored.isRecording) {[_audioRecored pause];fireDate = [SDate distantFuture];}
}#pragma mark 点击恢复按钮
- (IBAction)clickResumeBtn:(id)sender {[self clickRecordBtn:nil];
}#pragma mark 点击停止按钮
- (IBAction)clickStopBtn:(id)sender {[_audioRecored stop];fireDate = [SDate distantFuture];
}#pragma mark 协议中方法,录音结束
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{SLog(@录音结束);if (!self.audioPlay.isPlaying) {[_audioPlay play];}}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];
}@end

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/shuma/845682.html

相关标签:无
上传时间: 2024-02-05 13:06:12
留言与评论(共有 17 条评论)
本站网友 海南地产
27分钟前 发表
(id)sender {[self clickRecordBtn
本站网友 湖北科技学院地址
0秒前 发表
0];//取得第一个通道的音频,注意音频(声音)强度范围时-160到0
本站网友 菲克
18分钟前 发表
strong) AVAudioRecorder *audioRecored; //音频录音机@property (nonatomic
本站网友 南京熊猫电子
20分钟前 发表
self selector
本站网友 汤玉祥
10分钟前 发表
24
本站网友 浙江最新油价
25分钟前 发表
(AVAudioRecorder *)recorder successfully
本站网友 女性潮吹
7分钟前 发表
UIViewController - (IBAction)clickRecordBtn
本站网友 男性结扎手术视频
4分钟前 发表
2[dic setObject
本站网友 原地跑步减肥方法
29分钟前 发表
nil]; }#pragma mark 获得音频的URL -(SURL *)getAudioUrl {SString *documentPath = [SSearchPathForDirectoriesInDomains(SDocumentDirectory
本站网友 城市规划法
6分钟前 发表
nil];[audioSession setActive
本站网友 路易十三酒店
18分钟前 发表
@(8) forKey
本站网友 中华人民共和国拍卖法
8分钟前 发表
(id)sender {[self clickRecordBtn
本站网友 视力测试
6分钟前 发表
@(8000) forKey
本站网友 office2007兼容包下载
24分钟前 发表
@selector(aduioPowerChange) userInfo
本站网友 玉米煮多久就熟了
9分钟前 发表
0.1 target
本站网友 一桥大学
13分钟前 发表
2[dic setObject