我正在使用MPMusicPlayerController
实现一个在前台和后台播放歌曲的iOS应用。当我在前台时,我可以使用下面的代码转到下一首歌。
long currentPlaybackTime = musicPlayer.currentPlaybackTime;
//NSLog(@"%lu",currentPlaybackTime);
int currentHours = (currentPlaybackTime / 3600);
int currentMinutes = ((currentPlaybackTime / 60) - currentHours*60);
int currentSeconds = (currentPlaybackTime % 60);
// The total duration of the track...
long totalPlaybackTime = ([[[musicPlayer nowPlayingItem] valueForProperty: @"playbackDuration"] longValue])-currentPlaybackTime;
int tHours = (totalPlaybackTime / 3600);
int tMins = ((totalPlaybackTime/60) - tHours*60);
int tSecs = (totalPlaybackTime % 60);
float a = currentPlaybackTime;
float b = [[[musicPlayer nowPlayingItem] valueForProperty: @"playbackDuration"] floatValue];
float fltTime= a / b;
self.positionSlider2.value = fltTime;
if(tHours == 0 && tMins == 0 && tSecs == 0)
{
[self nextSong:nil];
}
但是当我在幕后为什么因为计时器不在后台工作时我不能去下一首歌。我试图使用
MPMusicPlayerControllerPlaybackStateDidChangeNotification
,但这也只是在前台工作。当应用程序在后台时,有人能告诉我如何去下一首歌。如果我不清楚,请告诉我。 [self addObserver:self forKeyPath:@"musicPlayer.currentPlaybackTime" options:0 context:nil]
但这不适用于前景和背景。
任何想法/建议都可能非常有用。