1

Hi there I'm using KVO to observe AvPlayer states, i am using instances of AVPlayer in different controllers, and observing them usign KVO.(Ex home controller has player and its observers and category controller has player and its observers, etc). I have a centralized class of Constants where i put the obsever contests like :

static void const *kCurrentItemDidChangeKVO  = &kCurrentItemDidChangeKVO;
static void const *kRateDidChangeKVO         = &kRateDidChangeKVO;
static void const *kStatusDidChangeKVO       = &kStatusDidChangeKVO;

in each controller i add observers like:

 if (player != nil)
    {
        [player addObserver:self forKeyPath:@"rate"                            options:NSKeyValueObservingOptionNew context:&kRateDidChangeKVO];
        [player addObserver:self forKeyPath:@"currentItem.status"              options:NSKeyValueObservingOptionNew context:&kStatusDidChangeKVO];
        [player addObserver:self forKeyPath:@"currentItem.duration"            options:NSKeyValueObservingOptionNew context:&kDurationDidChangeKVO];
    }

and remove observers in each controller like:

if (player != nil)
    {
            [player  removeObserver:self forKeyPath:@"rate" context:&kRateDidChangeKVO];
            [player  removeObserver:self forKeyPath:@"currentItem.status" context:&kStatusDidChangeKVO];
            [player  removeObserver:self forKeyPath:@"currentItem.duration" context:&kDurationDidChangeKVO];
    }

and in each AVPlayer i add and remove observers when that class(ViewController) is viewed, visited, viewWillAppeard or viewWillDissapeared using same contexts from Constants file.

My Question is: It is fine using same contexts from a centralized Constants file for each class containing player observer or should each class have their own contexts unique for that class?.

Any help is very appreciated. Regards

Aragunz
  • 511
  • 5
  • 18
  • The best use would be to create a global AVPlayer and its KVO and use in application as it would easy to manage in background and foreground state of application rather than creating a new AVPlayer and its KVO for each view controllers which is bad pratice of usage. – Paresh Navadiya Oct 29 '15 at 12:58

1 Answers1

1

Best use : To create a global AVPlayer and its KVO and use in application as it would easy to manage in background and foreground state of application and easy to use.

Note : At a time only one sound/video can be played so even if its different for different view controllers then use same global AVPlayer to do so.

How it can be done ?

Use global AVPlayer's instance in different view controllers with simple rule is that remove AVPlayer's view added before using in different view controller where its being added again.

Bad pratice of usage : Creating a new AVPlayer and its KVO for each view controllers which will be difficult to handle.

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • 1
    Thanks for info ,here is the deal, i have a tab based app the AVPlayer should play video in first tab if i go to tab 3 i have to play another video asap controller appears and pause player in first tab when controller dissapears , it is possible this global creating of AVPlayer and its KVO this way ? Plz let me know so i can accept the solution you offered. – Aragunz Oct 29 '15 at 13:17
  • yes its possible to use global AVPlayer's instance in different view controllers with simple rule is that remove AVPlayer's view added before using in different view controller where its being added again. – Paresh Navadiya Oct 29 '15 at 13:31
  • thank you very much, do you have an actual sample code i could refer to, that would be very very helpfull. :) – Aragunz Oct 29 '15 at 13:33
  • 1
    No i don't have any sample code but i have done this in one application where it plays video in background and anywhere in application where you can find current playing video. – Paresh Navadiya Oct 29 '15 at 13:36
  • 1
    I think https://github.com/rFlex/SCRecorder/blob/master/Library/Sources/SCPlayer.m SCPlayer is great example :), don't you agree :). accepting ur right answer. – Aragunz Oct 29 '15 at 13:38