IOS development: audio session

Source: Internet
Author: User

In iOS development, the most important framework for audio and video is the avfoundation framework. Starting from this article, we will focus on avfoundation-based audio usage and processing. To learn how to use audio, you must first understand the audio session.


1. What is an audio session?

First, give an official explanation in English:

An audio session is
The intermediary between your application and IOS for processing audio behavior. Upon launch, your application automatically gets a singleton audio
Session. You configure it to express your application's audio intentions.

In my understanding, the most basic audio configuration of an application is used to set the audio processing mode between the basic application and the system. For example:

1) Do I Need To mix audio from other applications during audio playback? Or mute other audios?

2) Do I need to pause the audio in the app when the IOS alarm is triggered?

3) How should the application respond when the user unplugging the headset? Do you want to stop music when you unplug the headset?

4) is the audio paused after the screen is disabled?

These problems are the things to be set for the audio session. You can say that you want to use avfoundation to play music. The first step is to set the audio session, which affects the playing mode of related audio in the application.


2. Audio session setting practices

1) fundamental method of setting: Change the category of the audio session.

An audio session category is a key used to describe a type of audio processing methods. You can change the configuration by setting different categories for the audio session.

In general, category has the following four categories:

(1) Whether sound mixing is allowed

(2) enable or disable mute when you enable or disable the screen.

(3) Whether audio input is supported, such as recording audio

(4) Whether audio output is supported, such as playing audio


2) What is the default audio session?

If your application is exactly the default setting of the symbol audio session, you do not need to set it specially. The default audio session has the following settings:

(1) allow playing playback and not allow recording;

(2) When you enable the mute function on the device side, the audio being played in the application will be muted;

(3) When the user locks the screen or the screen is automatically dimmed, the audio being played in the application will be muted;

(4) When the audio in your app starts, if other apps such as the iPod are playing music at this time, the iPod will pause playing.

The above methods have been packaged into a category, avaudiosessioncategorysoloambient, the default category.


3) The audio session does not work.

Two types:

(1) Audio is not used in applications. (Isn't that white ?)

(2) The application uses system sound services or playinputclick of uikit.

In the previous article, the latter is to set the keyboard sound for clicking the keyboard.


4) start setting audio SESSION (prerequisite: Add avfoundation. Framework to xcode

(1) obtain the audio Session Object

Avaudiosession * session = [avaudiosession sharedinstance];


(2) Basic settings

Use setcategory: Error: Or setcategory: Option: Error: to implement

For example:

[Session setcategory: kaudiosessioncategoryplayback error: Nil];

(3) Introduction to category

// The following category is used for audio secondary applications. The audio of an application is mixed with the audio of other applications.

// Disable the screen or enable the mute function.

Nsstring * const avaudiosessioncategoryambient;

// Default category

Nsstring * const avaudiosessioncategorysoloambient;


// The following category focuses on audio playback. When this category is used, the screen or mute switch is disabled.

// The audio will continue to be played, and the audio will not be mixed with other applications. Related settings are required to enable the application to continue in the background.

Nsstring * const avaudiosessioncategoryplayback;


// The application of this category will focus on recording. Mute the played audio. When the screen is off, the recorded audio continues.

// Related settings are required to enable the application to continue in the background.

Nsstring * const avaudiosessioncategoryrecord;


// The following category applications allow audio playback and recording, for example, for VOIP voice calls. Playback and recording

// It can be performed at the same time. If you close the screen, you can continue. Related settings are required to enable the application to continue in the background.

Nsstring * const avaudiosessioncategoryplayandrecord;


// The following category is used for audio processing. For details, see the development documentation.

Nsstring * const avaudiosessioncategoryaudioprocessing;


// The following category can achieve multiple outputs at the same time. For details, see the development documentation.

Nsstring * const avaudiosessioncategorymultiroute;


4. Summary

The preceding settings are the most basic settings of the audio session. You can see many other settings in the development documentation. If you want to develop an advanced audio application, you can go to the development documentation.






Related Article

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.