Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS 5 - play audio file in the background when the app is launched
    text
    copied!<p>I'm new to iOS and developing an app. I've been searching all over to get a simple example on how to play an audio file in the background when an app is launched. Since the app that I'm developing is related to music lessons, I need this audio to be playing when the home screen is active. This is what I've done so far... </p> <ol> <li>Created a home screen (UIViewController) with some background images and a button to navigate further into the app.</li> <li>Set up "Required background mode" in the .plist</li> <li>Add the audio file into the project</li> <li><p>In my controller I have the following code block.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; //Set some labels here //Set a background image NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"shrutiFsharp" ofType:@"mp3"]]; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [audioPlayer play]; } </code></pre></li> </ol> <p>All the labels, background image, buttons show up fine.. and I'm able to navigate to another view. But when I'm on the home screen, I just cannot get the audio file to play. I'm running the iPhone 5.0 simulator.</p> <p>I don't see any error messages in the console. </p> <p>The code you see here is from an example that was already posted.. but I'm not quite sure why this does not work. I'm probably missing something dearly. Any help is appreciated.</p> <p>-KD</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload