20 January, 2010

Tricks

Trick1: When I implemented my custom ipod player in my game I have scheduled a timer to update playback duration(using UILabel), it worked fine but when I started playing game I saw a huge frame rate drop.I found that it is because my timer but it took me 3 hours to have a mechanism to start timer before displaying view and to stop the timer while going to another view.
Trick here is we have to call invalidate(to stop timer) in the same thread.And we can fire a new timer from any where so my final code was like this


-(void) stopPlayBackTimer
{
stopMyTimer = true;
}

-(void) startPlayBackTimer
{
playBackTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSongPlayBack:) userInfo:nil repeats:YES];
}

-(void) updateMusicPlayerProgress
{
if(stopMyTimer)
{
stopMyTimer = false;
[playBackTimer invalidate];
playBackTimer = nil;
}
MPMediaItem* currentMediaIte = [musicPlayer nowPlayingItem];
long currentPlayBackTime = [musicPlayer currentPlaybackTime];
long totalPlayBackTime = [[currentMediaIte valueForProperty:MPMediaItemPropertyPlaybackDuration] longValue];
float progress = (float)currentPlayBackTime/totalPlayBackTime;
[songProgressIndicator setValue:progress animated:YES];
}

No comments:

Post a Comment