Question

I'm building an application that uses an AVQueuePlayer to stream audio. I have my AVAudioSession set up with the category AVAudioSessionCategoryPlayback and I'm able to receive events from remoteControlReceivedWithEvent:. However, when I pause my audio session, the icon in the status bar does not disappear, nor does the pause/play button in the App Switcher change to play. The audio pauses, but the icons never change. Do you know why this might be happening?

I also use an MPMusicPlayerController to play music from the iPod music player. Both players are always initialized, however only one plays at a time. When the app opens, if I play something from the MPMusicPlayerController first, the AVQueuePlayer doesn't have this pause conflict anymore. It only has this problem when you play from the AVQueuePlayer first. I included the code to show you what I'm doing.

FYI- My MPMusicController is self.musicPlayer. My AVQueuePlayer is self.radioPlayer. After pausing, the status of my AVQueuePlayer is AVPlayerStatusReadyToPlay.

- (void)viewDidLoad {

    [super viewDidLoad];
    self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    self.musicPlayer.nowPlayingItem = self.currentSong;
    self.musicPlayer.shuffleMode = MPMusicShuffleModeSongs;
    self.musicPlayer.repeatMode = MPMusicRepeatModeAll;

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:nil error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[_radioPlayer currentItem]];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self resignFirstResponder];

    if (self.source == LIBRARY)
    {
        [self.parentDelgate reloadInformation];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    if (self.source == RADIO)
    {
        // Code that sets the URLs for the AVQueuePlayer

        [self initializeMusicPlayer];

        [self prepareRadioPlayer];

    }
    else if ( self.source == LIBRARY ) {
        if ( self.currentSong != self.musicPlayer.nowPlayingItem || self.playlist != nil)
        {
            [self initializeMusicPlayer];
            self.musicPlayer.nowPlayingItem = self.currentSong;
        }
    }
}

- (void)initializeMusicPlayer {
    if ( self.source == LIBRARY )
    {
        [self.musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:(NSArray *)self.tracks]];

        if ([self.radioPlayer rate] != 0.0)
            [self.radioPlayer pause];

        [self.musicPlayer play];
        self.status = PLAYING;
    }
    else if ( self.source == RADIO )
    {
        if ( self.musicPlayer.playbackState == MPMusicPlaybackStatePlaying )
            [self.musicPlayer pause];
    }
}

- (IBAction)pausePressed:(UIButton *)sender {
    if ( self.source == LIBRARY )
    {
        [self.musicPlayer pause];
    }
    else if (self.source == RADIO)
    {
        [self.radioPlayer pause];
    }

    [self onStatePaused];
}

- (void)onStatePaused {
    // UI Stuff
}

- (void)endSession {
    [self.musicPlayer stop];
    [self.radioPlayer pause];
    [self.radioPlayer removeAllItems];

    // Present different view
}

Any ideas?

Was it helpful?

Solution

I figured out the problem. I was adding multiple instances of observers to the AVQueuePlayer/AVPlayerItems which was interfering with this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top