Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3491604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:39:26+00:00 2026-05-18T11:39:26+00:00

So I have an application and I want to keep it working even if

  • 0

So I have an application and I want to keep it working even if the screen is turned off.
Previously when I wanted to do that I used this hack/trick – I play a silent/empty sound in a loop in the background (AudioServicesPlaySystemSound) so if user presses the on/off button the application still works in the background – so it never allow iPhone to go to sleep mode – it just turned off the screen and maybe things like wifi or bluetooth (and on the iPod Touch accelerometers as far as I remember). And it worked. I wanted to use the same trick in my new application but when I was testing it now it seems it doesn’t work anymore. The sound in the background plays (when I replace “empty” audio file with some sound I can hear it play) even with screen turned off but the sound it should play (using AVAudioPlayer) doesn’t play (even when I turn the screen on again).
I don’t know at which point it stopped working (it worked on 3.x OS for sure). Am I doing something wrong? Did Apple changed/fixed the “hack” that allowed your app to work even with screen turned off? Is there another way to allow the device to go to sleep (and drain the battery less) but continue to work?

This is the code I use to play background/silent sound:

-(void) playSilentSound
{ 
 CFBundleRef mainBundle = CFBundleGetMainBundle ();
 CFURLRef silentUrl = CFBundleCopyResourceURL (mainBundle, CFSTR ("silence"), CFSTR ("aiff"), NULL);
 AudioServicesCreateSystemSoundID (silentUrl, &silentSound);
 silentTimer = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(playSilence) userInfo: nil repeats: YES];
}

-(void) playSilence
{
 AudioServicesPlaySystemSound (silentSound);
}

And this is how I play the sound that should play even if the screen is turned off:

-(BOOL) playSound: (NSString *) path withLoops: (BOOL) loops stopAfter: (int) seconds
{
 NSError *error;
 player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: &error];
 player.delegate = self;
 player.numberOfLoops = 0;
 player.volume = volume;
 secondsPlayed = 0;
 loop = loops;
 BOOL played = [player play];

 if(played && seconds > 0)
 {
  timer = [[NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector: @selector(stopPlaying:) userInfo: [NSNumber numberWithInt: seconds] repeats: YES] retain];
  secondsLimit = seconds;
 } else {
  secondsLimit = -1;
 }
}

-(void) stopPlaying:(NSTimer*)theTimer
{
 if(secondsLimit > 0 && (secondsPlayed + [player currentTime]) >= secondsLimit)
 {
  [player stop];
  [timer release];
  timer = nil;
 }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)sender successfully:(BOOL)flag
{
 if(sender == player)
 {
  if(flag) { secondsPlayed += sender.duration; }
  if(loop) { [player play]; }
 }
}

The code is a bit complicated maybe – it could be just the first few lines – it’s made that way so you can play only X seconds of sound (if sound is shorter than X it will play few loops until the total time is >= X). And of course everything is working fine when the screen is left on.

Also – if you find the code useful in your projects (like playSound:withLoops:stopAfter:) – feel free to use it (but it would be cool if you send me a message so I would know that I helped :)).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:39:26+00:00Added an answer on May 18, 2026 at 11:39 am

    So the answer is to set session category. Some categories just turn off sound when the device is being locked. For me the best option was AVAudioSessionCategoryPlayback

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this application that I want to support multi languages. I thought the
I have a working web application that uses username/password SpringSecurity configuration. Now I want
I have a MVVM application and I want to keep track of the focused
We have an application that we want to make available to the general internet-using
I have been working on ipad application. In this application I have several views.
Complete C# newbie here, so cut me some slack. I have this application that
I have an application and I want to move some of the tables from
I have an application where I want to simulate the connection between a device
i have application who use specific process during running and i want to sample
I have an application which is a portal application and I want to allow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.