How to reduce audio file size in IOS
Asked Answered
M

2

6

I am doing an application which get songs from mediapicker and saving it to my application.i want to reduce the size of file,but i got a sample named "AACConverter",i test the application but it is not reducing the file size.could any one help me in solving this problem.

- (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;
}
Mcsweeney answered 27/6, 2012 at 12:48 Comment(5)
you could post some codeGorges
hi jimpic thanks for your response..here is the codeMcsweeney
You could format the code properly.Dicot
It's hard to tell without being able to see the code of TPAACAudioConverter.If your converting from uncompressed format to compressed you should see a change in file size. Are you doing this? If you want to reduce the file size from format 'a' to format 'a' you should look at ASBD. They allow you to define things such as bitrate.Unveiling
HI dubbat it solved but now again it creating problem i.e not converting the Itunes Songs(paid one,downloaded from itunes),they are not compressing getting error.Error: ExtAudioFileOpenURL failed (-43) DoConvertFile failed! -43 please help me in solving this.Mcsweeney
E
1

There are various ways in which you can solve this problem. In case you want the audio to play in background, you can use

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

By using this, you can use a small audio clip which will be small in size, and play it in a loop. Same thing can be applied to Effect Audio as well.

Apart from that, decreasing the Bitrate of audio clip also helps in decreasing the size of file. For all these editing operations, which includes editing/clipping/trimming sound clip, decreasing bitrate, saving in different formats, I'd suggest you use

Audacity (Open-source, free)

Elboa answered 5/10, 2012 at 6:30 Comment(0)
S
0

If you care about the size over the quality, maybe you want to conver the song to amr format.

Silk answered 27/7, 2012 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.