开发者

Combining Audio Files as One Properly on iPhone with NSData

开发者 https://www.devze.com 2022-12-08 15:08 出处:网络
I am using AVAudioRecorder to save an audio file every 15 seconds by stopping the recording appending that data to the previously recorder file using a NSMutableData object and using the appendData me

I am using AVAudioRecorder to save an audio file every 15 seconds by stopping the recording appending that data to the previously recorder file using a NSMutableData object and using the appendData method.

I then record again in the same method so the recording is "continuous". The problem is as soon as you do [re开发者_如何学JAVAcorder stop] it saves/closes out the audio file at the url specified. This is fine because I can fill an NSData object with it and append it to my NSMutableObject and then write to the same file url.

The problem is something must be wrong with the header info because its only playing the first part of the audio.

My question is how do I properly combine files, even as simple as combining two audio files and having the AVAudioPlayer see it as one file. I am thinking the header information for maybe the file length/duration is messing things up when I append the second recording to the first recording.

Any ideas?


An audio file has a header which specifies how many samples are in the file and how long the data chunk should be. Any data appended to an existing file will be ignored. This is why you only hear the first recording.

You can't just append or concat audio files as if they were raw data. You have to extract the sample data chunk from each file, concat just the data, and then write a new file header with the fields for the data length and number of samples filled out correctly, and finally the concatenated sound data chunk itself.


The iPhone Audio API allows you to create a very large/long audio file without having to do anything too complicated. See the sample code SpeakHere, it creates a sound file that just grows with time, likely up to some very large size.

0

精彩评论

暂无评论...
验证码 换一张
取 消