Objective-c / IOS: what is the easiest way to play an audio file back

I struggled with this for a long time, and there are no examples of code that it makes on the network. Can someone help me?

My application uses AVFoundation to record sound. @ 16-bit depth, 2 channels, WAV

I can access bytes of audio, I just don't know how to undo it.

+6
source share
3 answers

In wave data samples alternate. This means that the data is organized as follows.

Sample 1 Left | Sample 1 Right | Sample 2 Left | Sample 2 right ... Sample n Left | Sample n right 

Since each pattern is 16-bit (2 bytes), the first 2-channel pattern (i.e., for the left and right) is 4 bytes in size.

Thus, you know that the last sample in the wave data block is as follows:

 wavDataSize - 4 

You can then load each sample at a time by copying it to another buffer, starting at the end of the recording and counting backward. When you get to the start of the wave data, you change the wave data and the playback will be canceled.

Edit: If you need easy ways to read wave files in iOS, refer to the Audio File Reference .

Edit 2:

 readPoint = waveDataSize; writePoint = 0; while( readPoint > 0 ) { readPoint -= 4; Uint32 bytesToRead = 4; Uint32 sample; AudioFileReadBytes( inFile, false, maxData, &bytesToRead &sample ); AudioFileWriteBytes( outFile, false, writePoint, &bytesToRead, &sample ); writePoint += 4; } 
+7
source

Assuming a one-piece WAV file, try memmapping the file and copy the samples in the reverse order, starting at the end of the file, to the audio queue buffers or RemoteIO during callbacks, using one of these APIs to play sound. Stop copying before you get to the WAV / RIFF header (usually 1 44 bytes).

0
source

To cancel the sound, why not use currentPlaybackRate in MPMediaPlayback (https://developer.apple.com/library/ios/#DOCUMENTATION/MediaPlayer/Reference/MPMediaPlayback_protocol/Reference/Reference.html)

0
source

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


All Articles