I am trying to modify an iOS app that can send audio files (securely/encrypted) to other iOS devices. It uses AVAudioRecorder to record the audio files, and it uses AVAudioPlayer to play back any received audio files.
I am now trying to modify the app to create and send files that are compatible with Android. I modified the existing AVAudioRecorder code to this:
[settings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
[settings setValue:[NSNumber numberWithInt:16] forKey:AVEncoderBitRateKey];
[settings setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[settings setValue:[NSNumber numberWithFloat:48000.0] forKey:AVSampleRateKey];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPath = [paths objectAtIndex:0];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd HH-mm"];
NSString *filename = [NSString stringWithFormat:@"%@.aac", [format stringFromDate:NSDate.date]];
The files created by this are playable on a Mac, on an iOS device, but not on an Android device. If the file extension were manually changed to end in ".m4a", then the file plays on Android.
(The advantage with the ".aac" format is that old versions of the app can receive these files and play them back — I've maintained backwards compatibility.)
So I made this change:
NSString *filename = [NSString stringWithFormat:@"%@.m4a", [format stringFromDate:NSDate.date]];
However, the resultant file (which is created with no errors from AVAudioRecorder) cannot be played on Macs nor on iOS devices. And I am waiting to hear whether it works on Android.
When AVAudioPlayer tries to open that file, it outputs:
Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn’t be completed. (OSStatus error 1685348671.)" Code 1685348671 -> "dta?": The file is malformed, not a valid instance of an audio file of its type, or not recognized as an audio file.
Merely renaming the file to have a ".aac" again doesn't change the fact that it can't be loaded into anything on the Mac or iOS.
Right now, it seems like the best solution would be to create a ".aac" file, and then have the Android end replace the ".aac" with ".m4a" and try to play it.
Question 1: Does anyone know why changing the filename extension can cause AVAudioPLayer to fail? BTW, the ".m4a" file I created above, can't be opened with anything I have on the Mac so I can't tell whether the problem is with the file format or the inability of iOS or Macs to read this file type.
For anyone curious, I put a copy of an ".aac" and a ".m4a" file from AVAudioRecorder, here:
https://dl.dropboxusercontent.com/u/14939586/2013-07-08%2013-24.m4a https://dl.dropboxusercontent.com/u/14939586/2013-07-08%2010-11.aac
Question 2: While I set AVAudioRecorder to one channel, the resultant file is stereo. Why? (At least that is what VLC says about the metadata)
Question 3: Is there anyway to specify AAC-LC for lossless encoding? Or can I only do so if I move from AVAudioRecorder to a lower level API?
How to open an M4A file. You can play M4A files with various audio players, including Microsoft Groove Music (Windows), Apple Music (macOS), Microsoft Windows Media Player (Windows), Adobe Audition (multiplatform), and VideoLAN VLC media player (multiplatform).
Android tablets and phones, plus Apple's iPhone, iPad, and iPod touch, function as M4A players, too, and can play the file directly from an email or website without needing a special app, regardless of whether it uses AAC or ALAC. Other mobile devices may have built-in support as well.
Go to the location of the M4A file, choose it, and click Open. The tool will check the file and upload it. Then you need to click the Repair Files button. Browse the saving location for the recovered file where you want to access the file after repairing it.
Although M4A files are made for QuickTime, they can also be played in Windows Media Player if the user has the correct codecs installed. The M4A codec can be easily obtained as a stand-alone software from the Internet and can also be found in many codec packages.
I had the same problem, I was getting NSOSStatusErrorDomain 560030580 when creating an AVAudioPlayer
with a local file.
The problem was not the file, or its format, it was because I was setting up the AVAudioSession
incorrectly.
I was calling setCategory(.playAndRecord)
and setActive(yes)
way before recording the audio, and then setCategory(.playback)
and setActive(yes)
when playing it, and I never called setActive(false)
.
According to Apple docs for some playback and recording apps the deactivation needs to happen explicitly so what I did was:
setCategory(.record)
and setActive(yes)
right before recording, and when finishing call setActive(false)
setCategory(.playback)
and setActive(yes)
right before playing the file, and when finishing call setActive(false)
Also, I found this https://www.osstatus.com/ which gives more information about the errors thrown when dealing with audio/files stuff.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With