NimbleKit - fast iOS app development

obj-c mix with video bg - impossible?

Problem discussions

obj-c mix with video bg - impossible?

Postby Mografi » Thu Mar 15, 2012 2:14 am

Help!

I'm using objective-c mix to play a video as a looping background to my app. The original version just played video randomly, which worked, and was only on the objective-c side.

Now I'm adding a user menu to select which video to play (bg1.m4v, bg2.m4v, etc). The problem is it doesn't load the video now. The javascript appears to pass the variable 'num' to the objective-c function (method?) ok, but for some reason it never loads the new video, even though the file path is valid. My understanding of objective-C is very limited, so, any pointers on why this is not working would help.

Here's what I'm doing in javascript:

Code: Select all
NKRegisterClass("MoviePlayerViewController");

function setWPnum(num)
{   
    CallNKitAction('playagainNK?className=MoviePlayerViewController&sync=yes&num=' + num);
}



and here is the objective-c code:

MoviePlayerViewController.h
Code: Select all
#import <UIKit/UIKit.h>
#import <MediaPlayer/MPMoviePlayerController.h>


@interface MoviePlayerViewController : UIViewController {
   UIView *viewForMovie;
    MPMoviePlayerController *player;

    NSDictionary* _parameters;
    UIWebView* _webView;
    NSString* _pageTitle;
    NSString* _lastReturnResult;
    NSMutableArray* arguments;
    //-(NSURL *)movieURL;

    NSString* _wpnum;
}

@property (nonatomic, retain) IBOutlet UIView *viewForMovie;
@property (nonatomic, retain) MPMoviePlayerController *player;

-(NSURL *)movieURL;

@property (retain) NSDictionary* _parameters;
@property (retain) UIWebView* _webView;
@property (retain) NSString* _pageTitle;
@property (retain) NSString* _lastReturnResult;
@property (retain) NSString* _wpnum;


@end



MoviePlayerViewController.m
Code: Select all
#import "MoviePlayerViewController.h"

@implementation MoviePlayerViewController

@synthesize player, viewForMovie;
@synthesize _parameters, _webView, _pageTitle, _lastReturnResult, _wpnum;


// Implement loadView to create a view hierarchy programmatically, without using a nib.
// START:viewDidLoad1
- (void)viewDidLoad {
    [super viewDidLoad];
   
     //used for randomly switching video when returning to app from another app or home screen
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playagain:) name:@"applicationDidBecomeActive" object:nil];

     _wpnum = @"1";

     self.player = [[MPMoviePlayerController alloc] init];
     self.player.repeatMode = MPMovieRepeatModeOne;
     self.player.controlStyle = MPMovieControlStyleNone;
     self.player.shouldAutoplay = YES;
     self.player.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);   
     self.player.scalingMode = MPMovieScalingModeFill;
     
     [self.viewForMovie addSubview:player.view];
     
}
// END:viewDidLoad1



-(void)playagain:(NSNotification *)notification
{
    //switch video when receiving 'applicationDidBecomeActive'
   
    self.player.contentURL = [self movieURL];
    [self.player play];
}



-(void) playagainNK
{
    //switch video based on user selected number

    self._wpnum = [self._parameters objectForKey:@"num"];
   
    self.player.contentURL = [self movieURL];
    [self.player play];

}


// START:movieURL
-(NSURL *)movieURL
{
    if ([self._wpnum isEqualToString:@"0"])
    {
        NSLog(@"random");
       
        int i = (arc4random()%3)+1;
        NSString *tmp = [NSString stringWithFormat:@"bg%d", i];
        NSBundle *bundle = [NSBundle mainBundle];
       
        NSString *moviePath =
        [bundle
         pathForResource:tmp
         ofType:@"m4v"];

        NSLog(@"movie path - %@", moviePath);

        if (moviePath)
        {
            NSLog(@"movie path is ok");
            return [NSURL fileURLWithPath:moviePath];

        }else{
            NSLog(@"movie path is not ok");
            return nil;
        }
       
    }
    else
    {
        NSLog(@"specific num - %@", self._wpnum);
         NSString *tmp = [NSString stringWithFormat:@"bg%@", self._wpnum];
        NSBundle *bundle = [NSBundle mainBundle];
       
       
        NSString *moviePath =
        [bundle
         pathForResource:tmp
         ofType:@"m4v"];
       
        NSLog(@"movie path - %@", moviePath);
        if (moviePath)
        {
            NSLog(@"movie path is ok");
            return [NSURL fileURLWithPath:moviePath];
           
        }else{
            NSLog(@"movie path is not ok");
            return nil;
        }
    }
}
// END:movieURL



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return NO;
}

- (void)didReceiveMemoryWarning {
   // Releases the view if it doesn't have a superview.
    //NSLog(@"memory warning movie view");
   [super didReceiveMemoryWarning];
   
   // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
   // Release any retained subviews of the main view.
   // e.g. self.myOutlet = nil;
    //NSLog(@"unloaded movie view");
}


#pragma mark -
#pragma mark automatic setter methods

- (void)setNKParameters:(NSDictionary*)parameters
{
   self._lastReturnResult = nil;
   self._parameters = parameters;
}

- (void)setNKCurrentPage:(NSString*)pageTitle
{
   self._pageTitle = pageTitle;
}

- (void)setNKWebView:(UIWebView*)webView
{
   self._webView = webView;
}

- (void)dealloc {
    //NSLog(@"dealoc movie view");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"applicationDidBecomeActive" object:nil];
    [super dealloc];
}

@end



I have been trying all day to figure out why the local notification "applicationDidBecomeActive" version works but the version called from NKit/javascript does not work...according to NSLog, the variable info is being received...so, really confused. :evil: :cry:
Mografi
 
Posts: 59
Joined: Wed Jun 10, 2009 3:01 am

Re: obj-c mix with video bg - impossible?

Postby jazzbass » Thu Mar 15, 2012 3:17 pm

You created a view controller, and added movieplayer on it as its subview.
Code: Select all
     [self.viewForMovie addSubview:player.view];
however, you did not showed that view controller as modal or subview on NK's _webview.
You just called a method inside an unshown and probably not corretly initiated viewcontroller.
Look at examples of OBJC/JS Mixes, how we load viewcontrollers.
User avatar
jazzbass
 
Posts: 139
Joined: Sun Jun 27, 2010 9:01 pm

Re: obj-c mix with video bg - impossible?

Postby Mografi » Thu Mar 15, 2012 6:00 pm

Thanks Jazzbass! I think I've got it now!

using this now works (and makes more sense):

Code: Select all
[_webView addSubview: player.view];  
[_webView sendSubviewToBack: player.view];
Mografi
 
Posts: 59
Joined: Wed Jun 10, 2009 3:01 am


Return to Troubleshooting

Who is online

Users browsing this forum: No registered users and 2 guests