How to reduce audio file size on iOS

I am making an application that receives songs from mediapicker and saves it in my application. I want to reduce the file size, but I have a sample called "AACConverter", I am testing the application, but it does not reduce the file size.could someone 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; } 
+6
source share
2 answers

There are various solutions to this problem. If you want the sound to play in the background, you can use

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

Using this, you can use a small audio clip that will be small in size and play it in a loop. The same can be applied to Effect Audio.

In addition, reducing the bitrate of an audio clip also helps reduce file size. For all these editing operations, which include editing / cropping / cropping an audio clip, reducing bitrate, saving in different formats, I suggest you use

Audacity (open source, free)

+1
source

If you care about size by quality, maybe you want to convert the song to amr format.

0
source

Source: https://habr.com/ru/post/919128/


All Articles