博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
录制音频
阅读量:6687 次
发布时间:2019-06-25

本文共 3889 字,大约阅读时间需要 12 分钟。

#import 
#import
@interface ViewController : UIViewController
{ BOOL _newRecordingAvailable;}@property (weak, nonatomic) IBOutlet UILabel *averageLabel;@property (weak, nonatomic) IBOutlet UILabel *peakLabel;@property (strong, nonatomic) AVAudioPlayer *player;@property (strong, nonatomic) AVAudioRecorder *recorder;@property (weak, nonatomic) IBOutlet UIButton *recordButton;@property (weak, nonatomic) IBOutlet UIButton *playButton;@property (strong, nonatomic) NSString *recordedFilePath;- (IBAction)toggleRecording:(id)sender;- (IBAction)togglePlaying:(id)sender;@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize recordButton = _recordButton;@synthesize playButton = _playButton;@synthesize averageLabel = _averageLabel;@synthesize peakLabel = _peakLabel;- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{    if (flags == AVAudioSessionInterruptionOptionShouldResume)    {        [player play];    }}- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withOptions:(NSUInteger)flags{    if (flags == AVAudioSessionInterruptionOptionShouldResume)    {        [recorder record];    }}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup dafter loading the view, typically from a nib.    self.recordedFilePath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"recording.wav"];    NSURL *url = [[NSURL alloc] initFileURLWithPath:self.recordedFilePath];    NSError *error;    self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:&error];    if (error)    {        NSLog(@"Error initializing recorder: %@", error);    }    self.recorder.meteringEnabled = YES;    self.recorder.delegate = self;    [self.recorder prepareToRecord];        [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateLabels) userInfo:nil repeats:YES];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)toggleRecording:(id)sender{    if ([self.recorder isRecording])    {        [self.recorder stop];        [self.recordButton setTitle:@"Record" forState:UIControlStateNormal];    }    else    {        [self.recorder record];        [self.recordButton setTitle:@"Stop" forState:UIControlStateNormal];    }}- (IBAction)togglePlaying:(id)sender{    if (self.player.playing)    {        [self.player pause];        [self.playButton setTitle:@"Play" forState:UIControlStateNormal];    }    else if (_newRecordingAvailable)    {        NSURL *url = [[NSURL alloc] initFileURLWithPath:self.recordedFilePath];        NSError *error;        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];        if (!error)        {            self.player.delegate = self;            [self.player play];        }        else        {            NSLog(@"Error initializing player: %@", error);        }        [self.playButton setTitle:@"Pause" forState:UIControlStateNormal];        _newRecordingAvailable = NO;    }    else if (self.player)    {        [self.player play];        [self.playButton setTitle:@"Pause" forState:UIControlStateNormal];    }}-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{    [self.playButton setTitle:@"Play" forState:UIControlStateNormal];}- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{    _newRecordingAvailable = flag;    [self.recordButton setTitle:@"Record" forState:UIControlStateNormal];}-(void)updateLabels{    [self.recorder updateMeters];    self.averageLabel.text = [NSString stringWithFormat:@"%f", [self.recorder averagePowerForChannel:0]];    self.peakLabel.text = [NSString stringWithFormat:@"%f", [self.recorder peakPowerForChannel:0]];}@end

 

转载地址:http://plhao.baihongyu.com/

你可能感兴趣的文章
hdu 1016 Prime Ring Problem
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(一)——MyBatis的基础知识
查看>>
如何把写好的文件放到提供的地址上提交代码
查看>>
Django 2.0 新特性
查看>>
Tornado Web 服务器
查看>>
精选7款绚丽的HTML5和jQuery图片动画特效
查看>>
【转载】桌面WPF中嵌入Unity3D(Standalone)引擎并实现socket通信
查看>>
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart
查看>>
Eclipse 项目有红感叹号、小红叉
查看>>
如何使用Dev C++调试(debug)c程序
查看>>
[算法模板]高斯消元
查看>>
BZOJ2242 计算器
查看>>
[javaSE] 集合框架(共性方法)
查看>>
StringBuild,StringFormat," "+" " 三种方法速度测试
查看>>
poj2018 Best Cow Fences
查看>>
SpringBoot之demo
查看>>
弹出窗口:PopupWindow 详解
查看>>
RabbitMQ延迟队列
查看>>
China中间件 微信公众号 和 helloweblogic qq群欢迎加入!
查看>>
Builder(建造者)模式
查看>>