Play audio files in IOS background

Source: Internet
Author: User

First, in the info.plist, you need to play back the audio content in the background:

Add Key Uibackgroundmodes

Tips: UIBackgroundModesaudio键 同时还能让app使用AirPlay播放流媒体

In addition to setting up the Avaudiosession in the project:

Avaudiosession *audiosession = [Avaudiosession sharedinstance]; Nserror *setcategoryerror = nil; BOOL success = [Audiosession setcategory:avaudiosessioncategoryplayback error:&setcategoryerror];if (!success) {/ * Handle the error condition */} nserror *activationerror = Nil;success = [Audiosession setactive:yes Error:&activati Onerror];if (!success) {/* Handle the error condition */}

The default category for Avaudiosession is Avaudiosessioncategorysoloambient, You need to set its category to Avaudiosessioncategoryplayback to play audio in the screen lock or mute state


The types of avaudiosession can be set as follows: Avaudiosessioncategoryambient:        background Play type, audio type that will mix with other music Avaudiosessioncategorysoloambient:    background playback type, other concerts stop playing Avaudiosessioncategoryplayback:       Play music file Avaudiosessioncategoryrecord:        use Avaudiosessioncategoryplayandrecord when recording:  Recording and need to play when using avaudiosessioncategoryaudioprocessing: codec when or not playing audio or not recording when using Avaudiosessioncategorymultiroute:     There can be multiple inputs or outputs, for example: USB and headphone output simultaneously, but not all input and output modes are supported
The input method only includes: Avaudiosessionportusbaudio avaudiosessionportheadsetmic avaudiosessionportbuiltinmic The output includes only: Avaudiosessionportusbaudio avaudiosessionportlineout avaudiosessionportheadphones AVAudioSessionPortHDMI Avaudiosessionportbuiltinspeaker




Play audio files in IOS background

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.