Reputation: 131
I have data for 100 videos in my app. I need every video playback time when it is paused or when it is stopped I need to store all the videos play back time using NSUserDefaults. How can I achieve this?
I tried this. It's working fine for displaying stopped duration, but I need to save stopped, paused, and full video duration in a single key value reference like array or dictionary.
-(void) moviePlayerPlaybackStateDidChange:(NSNotification*)notification {
NSLog(@"playbackDidChanged");
moviePlayer = notification.object;
MPMoviePlaybackState playbackState = moviePlayer.playbackState;
if(playbackState == MPMoviePlaybackStateStopped) {
NSLog(@"MPMoviePlaybackStateStopped at %f while the full length is %f", moviePlayer.currentPlaybackTime, moviePlayer.duration);
NSLog(@"video title %@",videoTitle);
[[NSUserDefaults standardUserDefaults] setDouble:moviePlayer.currentPlaybackTime forKey:videoTitle];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"User default value : %@", [[NSUserDefaults standardUserDefaults] objectForKey:videoTitle]);
} else if(playbackState == MPMoviePlaybackStatePlaying) {
NSLog(@"MPMoviePlaybackStatePlaying");
} else if(playbackState == MPMoviePlaybackStatePaused) {
NSLog(@"MPMoviePlaybackStatePaused : %f",moviePlayer.currentPlaybackTime);
[[NSUserDefaults standardUserDefaults]setDouble:moviePlayer.currentPlaybackTime forKey:videoTitle];
}
}
Upvotes: 3
Views: 1057
Reputation: 14164
Actually, when I think about this. If you know the location of each video, and you were set on adding these values to NSUserDefaults, I would store everything in an array of dictionaries. Then you only have one entry to the defaults rather than 100. And it should be easy to figure out which one you need.
Upvotes: 1