0

This is my first post and I have a problem that I am completely stumped on. I have a AVQueueplayer with multiple mp3's posted on a server.

The app plays and loops while the app is in the foreground. However, when I press the home button, queueplayer stops looping.

Here is the code I have so far

-(void)playselectedsong{

    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord     error:&sessionError];


    UInt32 doChangeDefaultRoute = 1;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);





     NSString* musicURL= [NSString stringWithFormat:@"%@%@", getMusicURL, [musicArray     objectAtIndex:0]];
     NSString* musicURL2= [NSString stringWithFormat:@"%@%@", getMusicURL, [musicArray objectAtIndex:1]];



     NSArray *queue = @[[AVPlayerItem playerItemWithURL:[NSURL URLWithString:musicURL]], [AVPlayerItem playerItemWithURL:[NSURL URLWithString:musicURL2]]];
     AVQueuePlayer *qplayer = [[AVQueuePlayer alloc] initWithItems:queue];
     self.queuePlayer=qplayer;
     [qplayer play];
     qplayer.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;

     [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[queue lastObject]];


    }


    - (void)playerItemDidReachEnd:(NSNotification *)notification {

     //  code here to play next sound file
         NSLog(@"playeritemdidreachend");
         [self playselectedsong ];

     }
Rajesh
  • 10,318
  • 16
  • 44
  • 64
  • What language are you using? (Be sure to indicate in both your post and your tags) XCode can be used for cocoa or objective-C. Even if it is obvious to some people what language it is there (i.e., I recognize cocoa), it is still important information to include. – CodeMouse92 Jan 13 '14 at 04:50
  • @JasonMc92 Cocoa is not a language. – Rudolf Adamkovič May 31 '14 at 09:00
  • Hm, good to know. Yeah, I must have misread something a while back. Still, it is an important factor whether it is leveraging that API. – CodeMouse92 May 31 '14 at 20:04

1 Answers1

0
  1. Adjust your project for playing audio in background (see here)
  2. Start playing via

    dispatch_async(dispatch_get_main_queue(), ^{
        [avqueueplayerinstance play];            
    });//end block`
    
dp_
  • 75
  • 1
  • 1
  • 6