iOS play audio for long time background run (rogue)

Source: Internet
Author: User

For iOS7, Apple supports several background operating modes, BACKGROUNDTASK,VOIP, background music, etc., depending on the official document is good.

I need to run a long-running timer in the background, so I can't get the app into the suspend state.

Heard long ago can be played by the background music to achieve, borrowed a bit, testing for several days, find out a more reliable solution:

First in

-(void) Applicationdidenterbackground: (uiapplication *) application{

}

Inside Application Backgroundtask

[[UIApplication sharedapplication] Beginbackgroundtaskwithexpirationhandler:nil]

Implement a permission that can run for several minutes.

Then write a timer real-time detection backgroundtimeremaining

-(void) tik{

if ([[UIApplication sharedapplication] backgroundtimeremaining] < 61.0) {

[[Ckaudiotool sharedinstance] playSound];

[[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:nil];

}

}

[[Ckaudiotool sharedinstance] playSound]; this piece of code is going to play a silent music, and the key point is

[[Avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayback withoptions: Avaudiosessioncategoryoptionmixwithothers Error:&error]

This way, background playback will not affect other programs to play music.

I run this timer every minute Tik function, if the background run time is less than a minute, then go to apply for a backgroundtask.

The Magical place is: Backgroundtask can not be in the program has entered the background when the application, you may use a music playing the false foreground status to apply, so you can continue to apply to the authority, also completed a long time backstage execution.

application example "classmate Alarm clock". (This can be through the apple audit??? )

iOS play audio for long time background run (rogue)

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.