回顾这一个月以来所学到的ios知识

来源:互联网 发布:紫峰秒杀软件 编辑:程序博客网 时间:2024/05/23 01:13

1.ios的开端---做一个手机端真实功能的录音机

由于开始转的移动端,尤其是ios,根本不知道怎么做,用实验室的苹果机,然后学长告诉我用xcode来编程,剩下的都是自己图书馆借书,网络查资料,老大要求在几天内做完,于是更是有了几个不眠之夜。

好了,开始回顾这个来练手的小项目。

这个小项目首先是在ViewController.m中搭好各个组件的框架,(老大要求用纯代码的形式完成,所以我从来不会去用什么xib文件,我问老大为什么这样做,老大说好维护)

- (void)viewDidLoad {

    [superviewDidLoad];

   

    dataSource = [selffilesOfPath];

NSLog(@"dataSource:%@",dataSource);

    

    tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0,50,375,500)];

    self.title =@"Record Voice List";

    [tableviewsetDelegate:self];

    [tableviewsetDataSource:self];

    [self.viewaddSubview:tableview];


    recordButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    recordButton.frame =CGRectMake(45,590,136,37);

    [recordButtonsetTitle:@"RecordVoice"forState:UIControlStateNormal];

    [recordButtonaddTarget:selfaction:@selector(recordAudio)forControlEvents:UIControlEventTouchUpInside];

    recordButton.backgroundColor = [UIColorredColor];

    [self.viewaddSubview:recordButton];

    

    playButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    playButton.frame =CGRectMake(193,590,136,37);

    [playButtonsetTitle:@"Play"forState:UIControlStateNormal];

    [playButtonaddTarget:selfaction:@selector(playAudio)forControlEvents:UIControlEventTouchUpInside];

    playButton.backgroundColor = [UIColorgreenColor];

    [self.viewaddSubview:playButton];

}


// 录音功能的初始化

- (void) initRecordVoice{


    [selfvoiceCheck];

    NSURL *soundFileURL = [NSURLfileURLWithPath:[[selfdocumentsPath]stringByAppendingString:[selfgenerateFileName]]];

    NSLog(@"soundFileURL:%@",soundFileURL);

    

    NSDictionary *soundSetting;

    soundSetting = [NSDictionarydictionaryWithObjectsAndKeys:

                    [NSNumbernumberWithFloat:44100.0],AVSampleRateKey,

                    [NSNumbernumberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey,

                    [NSNumbernumberWithInt:2],AVNumberOfChannelsKey,

                    [NSNumbernumberWithInt:AVAudioQualityHigh],

                    AVEncoderAudioQualityKey,nil];

    

    self.audioRecorder = [[AVAudioRecorderalloc]initWithURL:soundFileURLsettings:soundSettingerror:nil];

    //开启音量检测

    self.audioRecorder.meteringEnabled =YES;

    self.audioRecorder.delegate =self;

}

然后是存文件问题和为新文件命名,我是采用日期来为文件命名

// 设置录音文件的命名

- (NSString *)generateFileName{


    NSDate *today = [NSDatedate];

NSLog(@"%@",today);

    NSDateFormatter *df = [[NSDateFormatteralloc]init];

    [df setDateFormat:@"yyyyMMddHHmmss"];

    NSString *s1 = [df stringFromDate:today];

    //NSDate *date = [df dateFromString:s1];

    s1 = [s1 stringByAppendingString:@".caf"];

    NSString *name = [@"/"stringByAppendingString:s1];

NSLog(@"%@",name);

    return name;

}


// 重新加载数据源并刷新tableview

- (void)reloadTableView{


    dataSource = [selffilesOfPath];

    [tableviewreloadData];

}


// 控制录制音频的功能

- (void)recordAudio{


    if([self.recordButton.titleLabel.textisEqualToString:@"RecordVoice"]){

        [selfinitRecordVoice];

        [self.audioRecorderrecord];

        NSLog(@"%@",self.audioRecorder);

        [self.recordButtonsetTitle:@"StopRecord"forState:UIControlStateNormal];

        [self.playButtonsetEnabled:NO];

    }else{

        [self.audioRecorderstop];

        [self.recordButtonsetTitle:@"RecordVoice"forState:UIControlStateNormal];

        [self.playButtonsetEnabled:YES];

        [selfreloadTableView];

    }

}


// 控制播放音频的功能

- (void)playAudio{


    if([self.playButton.titleLabel.textisEqualToString:@"Play"]){

        [self.audioPlayerplay];

        [self.playButtonsetTitle:@"Pause"forState:UIControlStateNormal];

        [self.recordButtonsetEnabled:NO];

    }else{

        [self.audioPlayerpause];

        [self.playButtonsetTitle:@"Play"forState:UIControlStateNormal];

        [self.recordButtonsetEnabled:YES];

    }

}


// 返回数据的行数

- (NSInteger)tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger)section{

    

    return [dataSourcecount];

}


// 给列表的表格添加数据

- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    

    static NSString* identifier =@"basis-cell";

    UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

    

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

    }

    

    cell.textLabel.text = [dataSourceobjectAtIndex:indexPath.row];

    return cell;

}


// 给列表的行选中添加事件

- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    NSString* message =[@"/"stringByAppendingString:[dataSourceobjectAtIndex:indexPath.row]];

    

    //Load the new sound in the audioPlayer for playback

    NSURL *soundFileURL = [NSURLfileURLWithPath:[[selfdocumentsPath]stringByAppendingString:message]];

    self.audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:soundFileURLerror:nil];

    [self.audioPlayerplay];

    NSLog(@"audioPlayer:%@",self.audioPlayer);

    

}


// 设置标题栏的标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{


    return@"Play List";

}



//获取指定目录的文件,存在数组中

- (NSArray *)filesOfPath{

    

    NSFileManager *fm = [NSFileManagerdefaultManager];

    NSString *path = [selfdocumentsPath];

    NSArray *files = [fm contentsOfDirectoryAtPath:path error:nil];

    return files;

}


//获取指定目录的路径

- (NSString *) documentsPath{

    

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *documentsdir = [paths objectAtIndex:0];

    return documentsdir;

}

反思:

这个小项目是一个简单的单视图操作,所以也没有在AppDelegate里面写代码,这个小玩意纯是为了练手和ios入门,以前也没有接触过ios,而且老大要求纯代码,所以自己也是费了好大的劲才弄的,代码实现效果如图:

0 0
原创粉丝点击