Prevent iphone from going to sleep background playing music

Source: Internet
Author: User
Tags sleep

Step One: Locate the item in the Resource folder Info.plist add a
Array of Required background modes
and add an element below, whose value is
APP plays Audio

Step Two:

Need to import the appropriate framework and header files
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

Step Three:
Disable automatic lock screen when program is running

Step four:
avaudiosession *audiosession = [avaudiosessionsharedinstance];
Nserror *err =nil;
[Audiosession setcategory:avaudiosessioncategoryplayandrecorderror:&err];
if (err)
NSLog (@ "audiosession:%@%d%@", [Errdomain], [Errcode], [[erruserinfo]description]);

Err =nil
if (err)
NSLog (@ audiosession:%@%d%@ ", [Errdomain], [Errcode], [[erruserinfo]description]);


NSString * Musicfilepath = [[nsbundlemainbundle]pathforresource:@ ' Hatsune ミク-world is Mine ' oftype:@ "MP3"]; Create a music file path
Nsurl * musicurl= [[Nsurlalloc]initfileurlwithpath:musicfilepath];

Avaudioplayer * Theplayer = [[Avaudioplayeralloc]initwithcontentsofurl:musicurlerror:nil];

[Theplayersetvolume:1]; Set the volume size
theplayer.numberofloops = -1;//Set music playback times 1 to loop


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: 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.