Oc 錄音機 功能(系統方法)~dome

認識下面的類
AVAudioRecorder //音頻錄音機
    AVAudioPlayer //音頻播放器

![Uploading 20170628224501549_649336.png …]
NSTimer //定時器
2.佈局界面 連線
顯示時間
回放點擊
開始錄製
停止錄製
回放錄音….等操作

20170628224501549.png

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioRecorderDelegate>
{
    AVAudioRecorder *_audioRecoder;//音頻錄音機
    AVAudioPlayer *_avplayer;//音頻播放器
    NSTimer *_timer;//定時器
    int      _count;//保存數量
    BOOL     _isSwitch;//是否開關
}

//開始錄製屬性
@property (weak,nonatomic) IBOutletUIButton *btnStart;
//顯示時間
@property (weak,nonatomic) IBOutletUILabel *showTime;
//回放屬性
@property (weak,nonatomic) IBOutletUIButton *btnPlayBack;
//文件路徑
@property (nonatomic ,copy) NSString *documentsPath;

//開始錄製
- (IBAction)startRecording:(id)sender;
//停止錄製
- (IBAction)stopRecording:(id)sender;
//回放錄音
- (IBAction)playBackRecording:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isSwitch =YES;
}

//開始錄製
- (IBAction)startRecording:(id)sender {
    //判斷錄音控制器是否爲空或者正在錄製;
    if (_audioRecoder==nil &&_audioRecoder.isRecording)
    {
        //設置控制器停止錄製;
        [_audioRecoder stop];
        //設置按鈕的標題爲開始錄製;
        [_btnStart setTitle:@"開始錄製"forState:UIControlStateNormal];
        [_timer invalidate];
        _timer =nil;

    }else{
        _count =0;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(repeatShowTime:)userInfo:@"admin"repeats:YES];

#pragma mark 下面設置錄音的參數和錄音文件的保存路徑等信息
        //獲取音頻文件的保存路徑
        NSString *destinationStr = [[self documentsPath] stringByAppendingPathComponent:@"sound.wav"];
        NSURL *destinationUrl = [NSURL fileURLWithPath:destinationStr];
        //創建一個Dictionary,用於保存錄制屬性
        NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
        //設置錄製音頻的格式
        [recordSettings setObject:[NSNumber numberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];
        //設置錄製音頻的採樣率
        //        [recordSettings setObject:[NSNumber numberWithFloat:@"1".floatValue] forKey:AVSampleRateKey];
        //設置錄製音頻的通道數
        [recordSettings setObject:[NSNumber numberWithInt:(_isSwitch =/* DISABLES CODE */ (YES) ?2:1)]forKey:AVNumberOfChannelsKey];
        //設置錄製音頻採用高位優先的記錄格式
        [recordSettings setObject:[NSNumber numberWithBool:YES]forKey:AVLinearPCMIsBigEndianKey];
        //設置採樣信號採用浮點數
        [recordSettings setObject:[NSNumber numberWithBool:YES]forKey:AVLinearPCMIsFloatKey];
        NSError *recorderSetupError =nil;

#pragma mark 到這裏開始實例化錄音對象
        //初始化AVAudioRecorder
        _audioRecoder = [[AVAudioRecorder alloc] initWithURL:destinationUrl settings:recordSettings error:&recorderSetupError];
        _audioRecoder.delegate =self;
        [_audioRecoder record];
        //設置單個按鈕的狀態爲錄音
        [_btnStart setTitle:@"正在錄音"forState:UIControlStateNormal];
    }
}

//停止播放
- (IBAction)stopRecording:(id)sender {

    [_audioRecoder stop];
    [_btnStart setTitle:@"開始錄製"forState:UIControlStateNormal];
    //設置計時器爲初始值;
    if (_timer) {
        [_timer invalidate];
        _timer =nil;
    }
    _count =0;
    _showTime.text =@"00:00";
}

//回放錄音
- (IBAction)playBackRecording:(id)sender {

    //獲取音頻文件的保存路徑
    NSString *destinationString = [[selfdocumentsPath] stringByAppendingPathComponent:@"sound.wav"];
    NSURL *url = [NSURLfileURLWithPath:destinationString];
    //創建AVAudioPlayer對象
    _avplayer = [[AVAudioPlayeralloc] initWithContentsOfURL:urlerror:nil];
    //開始播放
    [_avplayerplay];
    _btnPlayBack.backgroundColor=[UIColorgreenColor];

}

//獲取Documents目錄路徑
-(NSString *)documentsPath{

    if (!_documentsPath) {

        NSArray *searchPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        _documentsPath = searchPath[0];

    }

    return_documentsPath;
}

#pragma mark- 錄製音頻的代理方法
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder
{
    NSLog(@"---->被中斷!");
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)aRecorder successfully:(BOOL)flag
{
    if(flag)
    {
        NSLog(@"---->錄製完成!!");
    }
}
- (void)repeatShowTime:(NSTimer *)tempTimer {

    _count++;
    //設置在文本框上顯示時間;
    _showTime.text = [NSStringstringWithFormat:@"%02d:%02d",_count/60,_count%60];
}

- (void)dealloc {  //銷燬NSTimer</span>
    if (_timer) {
        [_timerinvalidate];
        _timer =nil;
    }
}

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章