简体   繁体   中英

Recording 1 second audio ios

How can I record only one second audio sound without using file system and convert it to a byte array to send it by JSON?

NSDictionary *recordSettings = [NSDictionary
                                        dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithInt:AVAudioQualityMin],
                                        AVEncoderAudioQualityKey,
                                        [NSNumber numberWithInt:16],
                                        AVEncoderBitRateKey,
                                        [NSNumber numberWithInt: 1],
                                        AVNumberOfChannelsKey,
                                        [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                                        [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
                                        nil];

recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:recordSettings error:nil];

Else, I wanna know how size is this one second sound recorded and time.

Sample rate = 16,000 Hz (samples per second)
Bit rate = 16 (bits per sample)
Therefore size of 1 second sample = 16K x 16 = 256Kbits

To record one second, you can use
- (BOOL)recordForDuration:(NSTimeInterval)duration

If you want a fast start, you can prime the recording beforehand using
- (void)prepareToRecord

For recording at a specific time, use
- (BOOL)recordAtTime:(NSTimeInterval)time forDuration:(NSTimeInterval)duration
(which implicitly calls prepareToRecord )

To get an accurate time of recording you will need to use a lower-level API. If you are not worried about accuracy, you can log the time you call the above method or the time it is written to the filesystem.

If you don't want to use the filesystem then you will need to use a lower-level API where you can get at the bitstream using a callback. Apple's Core Audio, or one of the many C++ audio engines (eg PortAudio , FMod , Novocaine ) will let you do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM