2012-06-27 77 views
6

我正在做一個應用程序,它從mediapicker獲取歌曲並將其保存到我的應用程序中。我想要減小文件的大小,但我得到了一個名爲「AACConverter」的示例,我測試了應用程序,但它不減少文件大小。任何人都可以幫助我解決這個問題。如何減少IOS中的音頻文件大小

- (IBAction)convert:(id)sender { 
    if (![TPAACAudioConverter AACConverterAvailable]) { 
     [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"") 
            message:NSLocalizedString(@"Couldn't convert audio: Not supported on this device", @"") 
            delegate:nil 
          cancelButtonTitle:nil 
          otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show]; 
     return; 
    } 

    // Initialise audio session, and register an interruption listener, important for AAC conversion 
    if (!checkResult(AudioSessionInitialize(NULL, NULL, interruptionListener, self), "initialise audio session")) { 
     [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"") 
           message:NSLocalizedString(@"Couldn't initialise audio session!", @"") 
           delegate:nil 
         cancelButtonTitle:nil 
         otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show]; 
     return; 
    } 


    // Set up an audio session compatible with AAC conversion. Note that AAC conversion is incompatible with any session that provides mixing with other device audio. 
    UInt32 audioCategory = kAudioSessionCategory_MediaPlayback; 
    if (!checkResult(AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory), "setup session category")) { 
     [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"") 
           message:NSLocalizedString(@"Couldn't setup audio category!", @"") 
           delegate:nil 
         cancelButtonTitle:nil 
         otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show]; 
     return; 
    } 

    NSArray *documentsFolders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    audioConverter = [[[TPAACAudioConverter alloc] initWithDelegate:self 
                  source:[[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3"] 
                 destination:[[documentsFolders objectAtIndex:0] stringByAppendingPathComponent:@"audio.m4a"]] autorelease]; 



    NSLog(@"destinatiion path is %@",[[documentsFolders objectAtIndex:0]stringByAppendingFormat:@"audio.m4a"]); 
    ((UIButton*)sender).enabled = NO; 
    [self.spinner startAnimating]; 
    self.progressView.progress = 0.0; 
    self.progressView.hidden = NO; 

    [audioConverter start]; 
} 

- (IBAction)playConverted:(id)sender { 
    if (audioPlayer) { 
     [audioPlayer stop]; 
     [audioPlayer release]; 
     audioPlayer = nil; 
     [(UIButton*)sender setTitle:@"Play converted" forState:UIControlStateNormal]; 
    } else { 
     NSArray *documentsFolders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *path = [[documentsFolders objectAtIndex:0] stringByAppendingPathComponent:@"audio.m4a"]; 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
     [audioPlayer play]; 

     [(UIButton*)sender setTitle:@"Stop" forState:UIControlStateNormal]; 
    } 
} 

- (IBAction)emailConverted:(id)sender { 
    NSArray *documentsFolders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [[documentsFolders objectAtIndex:0] stringByAppendingPathComponent:@"audio.m4a"]; 

    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init]; 
    mailController.mailComposeDelegate = self; 
    [mailController setSubject:NSLocalizedString(@"Recording", @"")]; 
    [mailController addAttachmentData:[NSData dataWithContentsOfMappedFile:path] 
          mimeType:@"audio/mp4a-latm" 
          fileName:[path lastPathComponent]]; 

    [self presentModalViewController:mailController animated:YES]; 
} 

#pragma mark - Audio converter delegate 

-(void)AACAudioConverter:(TPAACAudioConverter *)converter didMakeProgress:(CGFloat)progress { 
    self.progressView.progress = progress; 
} 

-(void)AACAudioConverterDidFinishConversion:(TPAACAudioConverter *)converter { 
    self.progressView.hidden = YES; 
    [self.spinner stopAnimating]; 
    self.convertButton.enabled = YES; 
    self.playConvertedButton.enabled = YES; 
    self.emailConvertedButton.enabled = YES; 
    audioConverter = nil; 
} 

-(void)AACAudioConverter:(TPAACAudioConverter *)converter didFailWithError:(NSError *)error { 
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"") 
           message:[NSString stringWithFormat:NSLocalizedString(@"Couldn't convert audio: %@", @""), [error localizedDescription]] 
           delegate:nil 
         cancelButtonTitle:nil 
         otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show]; 
    self.convertButton.enabled = YES; 
    audioConverter = nil; 
} 
+0

你可以張貼一些代碼 – jimpic

+0

嗨吉姆皮克感謝您的回覆..有代碼 – girish

+2

您可以正確格式化代碼。 – Almo

回答

1

有多種方法可以解決此問題。 如果你想讓音頻在後臺播放,你可以使用

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@「BGM.mp3」loop:YES];

通過使用此功能,您可以使用體積較小的小型音頻剪輯並循環播放。同樣的事情也可以應用到效果音頻。

除此之外,降低音頻剪輯的比特率也有助於減小文件的大小。 對於所有這些編輯操作,其中包括編輯/剪切/裁切聲音片段,降低比特率,以不同格式保存,我建議你使用

Audacity(開源,免費)

0

如果您關心質量的大小,也許您想將歌曲轉換爲amr格式。