1

In an OSX-application I have a main class A and a second class B. Both run a window each, at the same time.

In class A there is a button that plays a movie in an AVPlayerView (called playerView) [The movie is loaded into the PlayerView earlier!]:

- (IBAction)runMovieN:(id)sender {

    [playerView.player play];

}

This works perfectly fine. The movie is played immediately after clicking the button.

On the other hand, in class B there is a button that calls a method in class A...

- (IBAction)runMovie:(id)sender {

    ViewController *runMovieButton = [[ViewController alloc] init];
    [runMovieButton runMovie];

}

...which will run this method in class A:

- (void)playPause {

    NSLog(@"A");
    [playerView.player play];

}

The problem is, that the Log will be printed out perfectly fine, but anything else that is in this method will be ignored, no matter what is in there. The content of the bracket is being ignored completely, except for the Log.

The solution is probably very easy, but I just cannot come up with any that could possibly work.

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • 1
    most probably the playView.player is null when calling from different class (: you can check it by NSLog(@"A%@", playerView.player); – hariszaman Jul 23 '15 at 21:07
  • My guess is that you're creating a whole new objectB (of class B) and not take the previous one. – Larme Jul 23 '15 at 21:42

1 Answers1

0

In your MainViewController.h

@interface MainViewController : UIViewController
-(void)playMedia:(NSURL*)url;
@end

In your MainViewController.m

@interface MainViewController ()
@property(nonatomic, retain) AVPlayerLayer *layer;
@property(nonatomic, retain)  AVPlayer *player;
@end

@implementation MainViewController

-(void)playMedia:(NSURL*)url{

    self.player = [AVPlayer playerWithURL:url];
    self.layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    self.layer.frame = CGRectMake(0, 0, 200, 200);
    [self.view.layer addSublayer: self.layer];

    [self.player play];
}

-(IBAction)playButton:(id)sender
{
    [self playMedia:[NSURL URLWithString:@"http://content.jwplatform.com/manifests/vM7nH0Kl.m3u8"]];
}

-(IBAction)moveToChild:(id)sender
{
    [self.layer.player pause];
    [self.layer removeFromSuperlayer];
    self.player=nil;

    ChildViewController* childView = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
    [self presentViewController:childView animated:YES completion:nil];
}

@end

In you ChildViewController.h

@interface ChildViewController : MainViewController

@end

In your ChildViewController.m

  -(IBAction)playButton:(id)sender{
    [self playMedia:[NSURL URLWithString:@"http://srv6.zoeweb.tv:1935/z330-live/stream/playlist.m3u8"]];
}

Please not if their is no Parent child relation in two classes you can also use delegates to interact between Viewcontrollers

hariszaman
  • 8,202
  • 2
  • 40
  • 59