Search code examples
iosxcodeavplayerviewcontrolleravassetmpmediaplayercontroller

How to play video from where i stopped previously. from remote server url


I am playing video from remote url. Suppose stopped playing the video and later again I am trying to play the same video. Now, I have to play from where i stopped previously. Like VLC Player


Solution

  • When you leave the ViewController in which you are playing the video, for example in the viewWillDisappear, you can save in the NSUserDefault the time value where you stopped playing the video (if there is still something to watch).

    Indeed, in the viewWillAppear then, you can check this value from the NSUserDefault every time before playing the video. So that, if you have a value for the key : "timeLeftPlaying" for example, you can load the video directly at this value.

    Getter and Setter in order to store or retrieve value from NSUserDefault :

    +(NSString *)getTimeLeftPlaying
    {
        return [[NSUserDefaults standardUserDefaults] objectForKey:@"timeLeftPlaying"];
    }
    
    +(void) setTimeLeftPlaying:(NSString *)timeLeft
    {
        [[NSUserDefaults standardUserDefaults] setValue:timeLeft forKey:@"timeLeftPlaying"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    }
    

    NB: I recommend you to create a custom class in order to regroup all the methods relative to NSUserDefault, so that you can access it easily from anywhere.

    If you never play around NSUserDefault I'd recommend checking this helpful links :

    Apple Official Documentation

    StackOverflow Example

    Hope it will help you and give you enough informations to start your feature.