0

Is it possible to play a track when the app has exited/gone into a background state?

For example:

   - (void)applicationDidEnterBackground:(UIApplication *)application
    {    
        NSURL *trackURL = [NSURL URLWithString:@"spotify:track:489K1qunRVBm2OpS4XGLNd"];
        [[SPSession sharedSession] trackForURL:trackURL callback:^(SPTrack *track) {

            if (track != nil) {

            [SPAsyncLoading waitUntilLoaded:track timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *tracks, NSArray *notLoadedTracks) {
                [self.playbackManager playTrack:track callback:^(NSError *error) {

                    if (error) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Play Track"
                                                                        message:[error localizedDescription]
                                                                       delegate:nil
                                                              cancelButtonTitle:@"OK"
                                                              otherButtonTitles:nil];
                        [alert show];
                    } else {
                        self.currentTrack = track;
                    }

                }];
            }];
        }
    }];
}

The above code was taken from the Simple Player app provided with cocoalibspotify.

Daniel Hakimi
  • 1,153
  • 9
  • 16

1 Answers1

0

Copying and pasting code like this won't get you far when backgrounding - you can't use UI elements like UIAlertView, for a start.

You need to declare your application as supporting the audio background mode as documented by Apple. Then, when you go into the background you should make a background task to start playback.

iKenndac
  • 18,730
  • 3
  • 35
  • 51