[Request] Coding movies into app

Discussion in 'iOS Development' started by Rockstarou812, Feb 9, 2009.

  1. Rockstarou812

    Rockstarou812 New Member

    Joined:
    Jan 17, 2009
    Messages:
    43
    Likes Received:
    0
    This should be easy, I have tried many times to get this to work, no success.

    Needs to be an m4v correct?

    Please share tutorial or maybe just a quick code?

    Thanks.
  2. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Code:
    
    -(void)awakeFromNib {
    	NSBundle *bundle = [NSBundle mainBundle]; //Use only if local video 
    	NSString *moviePath = [bundle pathForResource:@"video" ofType:@"m4v"];
    
    	NSURL *movieURL;
    	
    	if (moviePath)
    	{
    			movieURL = [NSURL fileURLWithPath:moviePath];
    	}
    	
    	if (movieURL != nil) {
    		moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    		
    		// Register to receive a notification when the movie has finished playing. 
    		[[NSNotificationCenter defaultCenter] addObserver:self
    												 selector:@selector(moviePlayBackDidFinish:) 
    													 name:MPMoviePlayerPlaybackDidFinishNotification 
    												   object:moviePlayer];
    		
    		moviePlayer.scalingMode = MPMovieScalingModeAspectFill; 
    		moviePlayer.movieControlMode = MPMovieControlModeDefault;
    		moviePlayer.backgroundColor = [UIColor blackColor];
    		
    		[moviePlayer play]; //Starts the video as soon as the application starts
    		
    	}
    	
    }
    
    -(void)moviePlayBackDidFinish: (NSNotification*)notification
    {
    	moviePlayer = [notification object];
    	[moviePlayer stop];
    }
    
    I put this in my MainView file. Also, remember to import the MediaPlayer.framework file.
  3. Rockstarou812

    Rockstarou812 New Member

    Joined:
    Jan 17, 2009
    Messages:
    43
    Likes Received:
    0
    Thank you, I will try this tonight.
  4. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    It should be noted that you have to define "MPMoviePlayerController *moviePlayer" in the Main View.h file and that this does not work on the Simulator. On first launch on my Device however, the movie started to play perfectly

    Please Register or Log in to view images

  5. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Well it works great in my simulator.
  6. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    It never loads the movie for me in the simulator. However, the loading screen does display and the controls overlay with the single tap.

    How did you get the movie to play in the Simulator?
  7. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    Hint - [bundle pathForResource

    Please Register or Log in to view images

    "video" ofType

    Please Register or Log in to view images

    "m4v"];

    He has video.m4v under Resources directory and under Resources under Xcode.

Share This Page