2013-03-16 47 views
0

我使用AVAudioRecorder錄製音頻,目前錄製10秒,音頻錄製成功,將其保存在本地目錄中,現在10秒的音頻需要1.8 MB的內存,我可以減少這個音頻文件的內存嗎?我使用下面的代碼來記錄音頻。AVAudioRecoder,減少錄製的音頻文件內存

-(void)viewWillAppear:(BOOL)animated{ 

NSArray *dirPaths; 
NSString *docsDir; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"]; 
path_audio = soundFilePath; 
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
[NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey, 
[NSNumber numberWithInt:16],AVEncoderBitRateKey, 
[NSNumber numberWithInt: 2],AVNumberOfChannelsKey, 
[NSNumber numberWithFloat:44100.0],AVSampleRateKey,nil]; 
NSError *error = nil; 
audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error]; 

if(!audioRecorder){ 
    NSLog(@"recorder: %@ %d %@", [error domain], [error code], [[error userInfo] description]); 
    UIAlertView *alert = 
    [[UIAlertView alloc] initWithTitle: @"Warning" message: [error localizedDescription] delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    return; 
     } 

if (error) 
     { 
    NSLog(@"error: %@", [error localizedDescription]); 
     } 
else { 

    [self recordAudio]; 
    } 

} 

-(void) recordAudio 
{ 
if (!audioRecorder.recording) 
    { 
     audioRecorder.delegate=self; 
     [audioRecorder recordForDuration:10]; 
    } 
    } 

任何答案應該感激。

回答

0

是的,我已經採取recordSettings perameters如,

NSDictionary *recordSettings = 
          [[NSDictionary alloc] initWithObjectsAndKeys: 
          [NSNumber numberWithFloat: 44100.0], AVSampleRateKey, 
          [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, 
          [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, 
          [NSNumber numberWithInt: AVAudioQualityMedium],AVEncoderAudioQualityKey, 
           nil]; 

//我的目的.. /*
它減少到33KB - (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:( NSInteger)部分 { NSInteger imageCount = [self.array_HotelImage count]/4;

return([self.array_HotelImage count]%4 == 0)?imageCount:imageCount + 1; }

//自定義表格視圖單元格的外觀。

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

{ 
static NSString *CellIdentifier = @"ImageGalleryCustomCell"; 

ImageGalleryCustomCell *objImageGalleryCustomCell = (ImageGalleryCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(objImageGalleryCustomCell == nil) 

{ 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ImageGalleryCustomCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects) 
    { 
     if ([currentObject isKindOfClass:[UITableViewCell class]]) 

     { 
      objImageGalleryCustomCell = (ImageGalleryCustomCell *) currentObject; 

      break; 
     } 
    } 
objImageGalleryCustomCell = [self hideAndShowControlWithData:objImageGalleryCustomCell andIndexpath:indexPath]; 

} 
return objImageGalleryCustomCell; 

}

- (ImageGalleryCustomCell*)hideAndShowControlWithData:(ImageGalleryCustomCell*)objImageGalleryCustomCell andIndexpath:(NSIndexPath*)indexPathValue 

{ 
int imageCount = [self.array_HotelImage count]; 
if(indexPathValue.row==imageCount/4) 

{ 
    switch(imageCount%4) 
    { 
     case 1: 

     { 
      objImageGalleryCustomCell.buttonImgGalleryView1.hidden = objImageGalleryCustomCell.imgGalleryView1.hidden = NO; 

      objImageGalleryCustomCell.buttonImgGalleryView2.hidden = objImageGalleryCustomCell.buttonImgGalleryView4.hidden = objImageGalleryCustomCell.buttonImgGalleryView3.hidden = objImageGalleryCustomCell.imgGalleryView2.hidden= objImageGalleryCustomCell.imgGalleryView3.hidden = objImageGalleryCustomCell.imgGalleryView4.hidden = YES; 

      objImageGalleryCustomCell.buttonImgGalleryView1.tag = (indexPathValue.row*4+0); 
      [objImageGalleryCustomCell.imgGalleryView1 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+0)]] placeholderImage:placholderImage]; 

      objImageGalleryCustomCell.buttonImgGalleryView1.tag = (indexPathValue.row*4+0); 
     } 

      break; 
     case 2: 
     { 

      objImageGalleryCustomCell.buttonImgGalleryView1.hidden = objImageGalleryCustomCell.buttonImgGalleryView2.hidden=objImageGalleryCustomCell.imgGalleryView1.hidden=objImageGalleryCustomCell.imgGalleryView2.hidden=NO; 

      objImageGalleryCustomCell.buttonImgGalleryView3.hidden = objImageGalleryCustomCell.buttonImgGalleryView4.hidden = objImageGalleryCustomCell.imgGalleryView3.hidden = objImageGalleryCustomCell.imgGalleryView4.hidden=YES; 

      objImageGalleryCustomCell.buttonImgGalleryView1.tag = (indexPathValue.row*4+0); 
      [objImageGalleryCustomCell.imgGalleryView1 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+0)]] placeholderImage:placholderImage ]; 

      objImageGalleryCustomCell.buttonImgGalleryView2.tag = (indexPathValue.row*4+1); 
      [objImageGalleryCustomCell.imgGalleryView2 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+1)]] placeholderImage:placholderImage]; 

     } 
      break; 
     case 3: 

     { 
      objImageGalleryCustomCell.buttonImgGalleryView1.hidden = objImageGalleryCustomCell.buttonImgGalleryView2.hidden= objImageGalleryCustomCell.imgGalleryView1.hidden=objImageGalleryCustomCell.imgGalleryView2.hidden=objImageGalleryCustomCell.buttonImgGalleryView3.hidden = objImageGalleryCustomCell.imgGalleryView3.hidden = NO; 

      objImageGalleryCustomCell.buttonImgGalleryView4.hidden =objImageGalleryCustomCell.imgGalleryView4.hidden = YES; 

      objImageGalleryCustomCell.buttonImgGalleryView1.tag = (indexPathValue.row*4+0); 
      [objImageGalleryCustomCell.imgGalleryView1 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+0)]] placeholderImage:placholderImage]; 

      objImageGalleryCustomCell.buttonImgGalleryView2.tag = (indexPathValue.row*4+1); 
      [objImageGalleryCustomCell.imgGalleryView2 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+1)]] placeholderImage:placholderImage]; 

      objImageGalleryCustomCell.buttonImgGalleryView3.tag = (indexPathValue.row*4+2); 
      [objImageGalleryCustomCell.imgGalleryView3 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+2)]] placeholderImage:placholderImage]; 

     } 
      break; 
    } 

} 
else 
{ 
    objImageGalleryCustomCell.buttonImgGalleryView1.tag = (indexPathValue.row*4+0); 

    [objImageGalleryCustomCell.imgGalleryView1 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+0)]] placeholderImage:placholderImage]; 

    objImageGalleryCustomCell.buttonImgGalleryView2.tag = (indexPathValue.row*4+1); 
    [objImageGalleryCustomCell.imgGalleryView2 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+1)]] placeholderImage:placholderImage]; 

    objImageGalleryCustomCell.buttonImgGalleryView3.tag = (indexPathValue.row*4+2); 
    [objImageGalleryCustomCell.imgGalleryView3 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+2)]] placeholderImage:placholderImage]; 

    objImageGalleryCustomCell.buttonImgGalleryView4.tag = (indexPathValue.row*4+3); 
    [objImageGalleryCustomCell.imgGalleryView4 setImageWithURL:[NSURL URLWithString:[self.array_HotelImage objectAtIndex:(indexPathValue.row*4+3)]] placeholderImage:placholderImage]; 

} 

[objImageGalleryCustomCell.buttonImgGalleryView1 addTarget:self action:@selector(buttonImageClicked:) forControlEvents:UIControlEventTouchUpInside]; 

[objImageGalleryCustomCell.buttonImgGalleryView2 addTarget:self action:@selector(buttonImageClicked:) forControlEvents:UIControlEventTouchUpInside]; 

[objImageGalleryCustomCell.buttonImgGalleryView3 addTarget:self action:@selector(buttonImageClicked:) forControlEvents:UIControlEventTouchUpInside]; 
[objImageGalleryCustomCell.buttonImgGalleryView4 addTarget:self action:@selector(buttonImageClicked:) forControlEvents:UIControlEventTouchUpInside]; 

return objImageGalleryCustomCell; 

} */