Use audio queue services to play and record audio

Source: Internet
Author: User

To play and record audio on iOS devices, we recommend that you use the avaudioplayer and avaudiorecorder classes in the avfoundation framework. Although its usage is relatively simple, streaming is not supported. This means that before playing the audio, you must wait until the whole audio is loaded to start playing the audio. During the recording, you must wait until the recording ends to obtain the recording data. This imposes great limitations on applications. To solve this problem, we needUse audio queue services to play and record audio;To simplify the processing of audio files, audio file services must be used here.(I used to think that the C language does not have a function library for audio file processing, and now I found it ).

 

Before using the service, we need to understand the basic working principles of the audioqueue service.

Figure 1Recording audio queue)

 

It is known that a recording audio queue, including a buffer (Buffer Queue) consisting of a buffer, and a callback (callback ). How do they work?

 

Figure 2Recording Process

1. Enter the audio in the first buffer.

2. When the first buffer in the queue is filled, the next buffer is automatically filled. The callback is triggered.

3. You need to write the audio data stream to the disk in the callback function.

4. Then, you need to re-place the buffer into the buffer queue in the callback function to reuse the buffer. Repeat Step 2.

 

After learning about the recording process, let's take a look at the basic architecture of playing.

Figure 3A playback audio queue)

It can be learned that its structure is basically the same as that of the recording audio queue, but the callback trigger time is different, and the workflow is slightly different.

 

 

Figure 4Playback Process

 

 

1. Read the audio into the cache. Once a buffer is filled up, it enters the cache queue and is on standby.

2. The Application Command sends a command to require the audio queue to start playing.

3. The audio will fetch data from the first cache and start playing.

4. Once the playback is complete, a callback is triggered and the content in the next cache is played.

5. In the callback, You need to fetch the audio data from the cache and then reload it into the cache queue. Repeat Step 3.

 

So far, the basic principles of audio queueservices have been described. In practical applications, you also need to handle various statuses and abnormal situations, such as playing interruption and no recording equipment. I will not introduce the use of audio file services here. For more information, see the source code.

Source code example with this article: http://download.csdn.net/detail/midfar/4044390

 

References:

Audio queue Services programming guide

Https://developer.apple.com/library/ios/#documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005343

Audio queue services reference

Https://developer.apple.com/library/ios/#documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/uid/TP40005117

Audio File Services reference

Https://developer.apple.com/library/ios/#documentation/MusicAudio/Reference/AudioFileConvertRef/Reference/reference.html#//apple_ref/doc/uid/TP4

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.