Prevent iphone from entering sleep background to play music

Source: Internet
Author: User

Step 1: find info. plist of the project in the resource folder and add
Array of Required background modes
And add an element below, whose value is
App plays audio

Step 2:

You need to import the corresponding framework and header files.
# Import <AudioToolbox/AudioToolbox. h>
# Import <AVFoundation/AVFoundation. h>


Step 3:
// Disable automatic screen lock when the program is running
[[UIApplicationsharedApplication] setIdleTimerDisabled: YES];


Step 4:
AVAudioSession * audioSession = [AVAudioSessionsharedInstance];
NSError * err = nil;
[AudioSession setCategory: AVAudioSessionCategoryPlayAndRecorderror: & err];
If (err)
{
NSLog (@ "audioSession: % @ % d % @", [errdomain], [errcode], [[erruserInfo] description]);

}
[AudioSessionsetActive: YESerror: & err];
Err = nil;
If (err)
{
NSLog (@ "audioSession: % @ % d % @", [errdomain], [errcode], [[erruserInfo] description]);

}




NSString * musicFilePath = [[NSBundlemainBundle] pathForResource: @ "ク-World Is Mine" ofType: @ "mp3"]; // create a music file path
NSURL * musicURL = [[NSURLalloc] initFileURLWithPath: musicFilePath];

AVAudioPlayer * thePlayer = [[AVAudioPlayeralloc] initWithContentsOfURL: musicURLerror: nil];

[ThePlayerplay];
[ThePlayersetVolume: 1]; // sets the volume.
ThePlayer. numberOfLoops =-1; // set the number of times the music is played-1 to keep repeating

[MusicURLrelease];
[ThePlayerrelease];

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.