Nil Value in remoteControlReceivedWithEvent
-
21-12-2019 - |
Вопрос
I have a master-detail app. Tapping on each cell of the tableView
in the MasterViewController
navigates to a DetailViewController
where you can download and play an audio file. I had problems keeping the AVAudioPlayer
instance, single, so that different files from multiple cells wouldn't play at the same time. Before fixing that, my remoteControlReceivedWithEvent
was working fine and I could manage events on all view controllers.
Fixing that issue now, my player plays only one file at the same time, so if I happen to navigate back to the MasterViewController
and then select another row and enter a different cell and start playing that, it will stop the first file and start the second. BUT the problem at the moment is that trying to use the control center while the user is on else where out of the DetailViewController
, remoteControlReceivedWithEvent
is called, but the value is nil
, so nothing happens. This is while I can detect which row is now playing in my cellForRowAtIndexPath:
and highlight it on the MasterViewController
as follows:
if (entry.audioManager && [entry.audioManager soundPlayer].isPlaying)
{
equalizer.hidden = NO;
[self animateTheEqualizer];
}
However I can't spot that in remoteControlReceivedWithEvent
since I don't have any access to the indices.
I have already included canBecomeFirstResponder
and so on, and I'm sure that it's only the confusion of the table view
understanding which file is currently being played. Everything works fine if I try this in the DetailViewController
but not outside it in another VC or even in the background.
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl)
{
if (event.subtype == UIEventSubtypeRemoteControlPlay)
{
[entry.audioManager playAudio];
// [[NIKDetailViewController sharedController].feedEntry.audioManager playAudio];
}
else if (event.subtype == UIEventSubtypeRemoteControlPause)
{
[entry.audioManager pauseAudio];
}
else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
{
[entry.audioManager togglePlayPause];
}
else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward)
{
[entry.audioManager rewindTheAudio];
}
else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward)
{
[entry.audioManager fastForwardTheAudio];
}
}
}
and in my AudioManager
class:
- (void)playAudio
{
//Play the audio and set the button to represent the audio is playing
if ([soundPlayer isPlaying])
{
[soundPlayer pause];
[self deleteTimer];
}
else
{
[self createTimer];
[soundPlayer play];
}
[self refreshButton];
// [[NIKMasterViewController sharedController] markEntryAsPlaying:detailViewController.feedEntry];
}
- (void)pauseAudio
{
//Pause the audio and set the button to represent the audio is paused
[soundPlayer pause];
[self deleteTimer];
[self refreshButton];
}
Should I markAsNowPlaying
manually or is there a delegate method I could use for this to spot the right index and avoid getting nil
?
Решение
Ok... Problem finally solved! It was something totally different that I needed to do. I'm just using the wrong object entry.audioManager
. I was supposed to use currentAudioManager
which is an extern
declared in AudioManager
class. That would keep the currently-played-file and won't be nil.