Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-04-2009, 12:50 PM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 23
MattTrout is on a distinguished road
Exclamation Memory Management with Video Playback

Hi All,

I've been looking around for the solution to my problem and nothing I seem to try works! I have 6 buttons that play 6 different video files. If I press one, it plays fine. If I press done and go back and press another button, it plays fine. But once I hit done on the second video, the app crashes and I get a "EXE_BAD_ACCESS" error. I know it has something to do with memory leaks and all that. I've tried releasing the String for the video URL, I've tried draining the pool and such. No Luck. Below is code. If you can share some insight or if you have run into this problem before and know what the hells going on I'd appreciate a response! Thank you!

Code:
- (NSURL *)videoOneURL {
	NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
	NSString *videoOnemoviePath = [[NSBundle mainBundle] pathForResource:@"videoOne" ofType:@"mov"];
	return [NSURL fileURLWithPath:videoOnemoviePath];
	[releasePool drain];
}

- (NSURL *)videoTwoURL {
	NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
	NSString *videoTwomoviePath = [[NSBundle mainBundle] pathForResource:@"videoTwo" ofType:@"mov"];
	return [NSURL fileURLWithPath:videoTwomoviePath];
	[releasePool drain];
}

- (NSURL *)videoThreeURL {
        NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
	NSString *videoThreecallmoviePath = [[NSBundle mainBundle] pathForResource:@"videoThree" ofType:@"mov"];
	return [NSURL fileURLWithPath:videoThreemoviePath];
        [releasePool drain];
}

- (NSURL *)videoFourURL {
        NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
	NSString *videoFourmoviePath = [[NSBundle mainBundle] pathForResource:@"videoFour" ofType:@"mov"];
	return [NSURL fileURLWithPath:videoFourmoviePath];
        [releasePool drain];
}

- (NSURL *)videoFiveURL {
        NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
	NSString *videoFivemoviePath = [[NSBundle mainBundle] pathForResource:@"videoFive" ofType:@"mov"];
	return [NSURL fileURLWithPath:videoFivemoviePath];
        [releasePool drain];
}

- (NSURL *)videoSixURL {
        NSAutoreleasePool *releasePool = [[NSAutoreleasePool alloc] init];
	NSString *videoSixmoviePath = [[NSBundle mainBundle] pathForResource:@"videoSix" ofType:@"mov"];
	return [NSURL fileURLWithPath:videoSixmoviePath];
        [releasePool drain];
}

-(IBAction)videoOnePlay:(id)sender
{
	moviePlayerObject = [[MPMoviePlayerController alloc] initWithContentURL:[self videoOneURL]];
	
	[moviePlayerObject play];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoOnePlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoOnePreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];
	
}

-(IBAction)videoTwoPlay:(id)sender
{
	moviePlayerObject = [[MPMoviePlayerController alloc] initWithContentURL:[self videoTwoURL]];
	
	[moviePlayerObject play];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoTwoPlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoTwoPreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];
	
}

-(IBAction)videoThreePlay:(id)sender
{
	moviePlayerObject = [[MPMoviePlayerController alloc] initWithContentURL:[self videoThreeURL]];
	
	[moviePlayerObject play];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoThreePlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoThreePreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];
}

-(IBAction)videoFourPlay:(id)sender
{
	moviePlayerObject = [[MPMoviePlayerController alloc] initWithContentURL:[self videoFourURL]];
	
	[moviePlayerObject play];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoFourPlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoFourPreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];
}

-(IBAction)videoFivePlay:(id)sender
{
	moviePlayerObject = [[MPMoviePlayerController alloc] initWithContentURL:[self videoFiveURL]];
	
	[moviePlayerObject play];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoFivePlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoFivePreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];
}

-(IBAction)videoSixPlay:(id)sender
{
	moviePlayerObject = [[MPMoviePlayerController alloc] initWithContentURL:[self videoSixURL]];
	
	[moviePlayerObject play];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoSixPlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(videoSixPreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];
}

-(void) videoOnePlayBackDidFinish:(NSNotification*)aNotification {
	NSLog(@"playback stop");
	moviePlayerObject = [aNotification object];
	
	
    [[NSNotificationCenter defaultCenter] removeObserver:self
	 
													name:MPMoviePlayerPlaybackDidFinishNotification
	 
												  object:moviePlayerObject];
	
    // Release the movie instance created in playMovieAtURL:
	
    [moviePlayerObject release];
}

-(void) videoOnePreloadDidFinish:(id)sender {
	NSLog(@"preload");
}

-(void)videoTwoPlayBackDidFinish:(NSNotification*)aNotification {
	NSLog(@"playback stop");
	moviePlayerObject = [aNotification object];
	
	
    [[NSNotificationCenter defaultCenter] removeObserver:self
	 
													name:MPMoviePlayerPlaybackDidFinishNotification
	 
												  object:moviePlayerObject];
	
    // Release the movie instance created in playMovieAtURL:
	
    [moviePlayerObject release];
}

-(void) videoTwoPreloadDidFinish:(id)sender {
	NSLog(@"preload");
}

-(void) videoThreePlayBackDidFinish:(NSNotification*)aNotification {
	NSLog(@"playback stop");
	moviePlayerObject = [aNotification object];
	
	
    [[NSNotificationCenter defaultCenter] removeObserver:self
	 
													name:MPMoviePlayerPlaybackDidFinishNotification
	 
												  object:moviePlayerObject];
	
    // Release the movie instance created in playMovieAtURL:
	
    [moviePlayerObject release];
}

-(void) videoThreePreloadDidFinish:(id)sender {
	NSLog(@"preload");
}

-(void) videoFourPlayBackDidFinish:(NSNotification*)aNotification {
	NSLog(@"playback stop");
	moviePlayerObject = [aNotification object];
	
	
    [[NSNotificationCenter defaultCenter] removeObserver:self
	 
													name:MPMoviePlayerPlaybackDidFinishNotification
	 
												  object:moviePlayerObject];
	
    // Release the movie instance created in playMovieAtURL:
	
    [moviePlayerObject release];
}

-(void) videoFourPreloadDidFinish:(id)sender {
	NSLog(@"preload");
}

-(void) videoFivePlayBackDidFinish:(NSNotification*)aNotification {
	NSLog(@"playback stop");
	moviePlayerObject = [aNotification object];
	
	
    [[NSNotificationCenter defaultCenter] removeObserver:self
	 
													name:MPMoviePlayerPlaybackDidFinishNotification
	 
												  object:moviePlayerObject];
	
    // Release the movie instance created in playMovieAtURL:
	
    [moviePlayerObject release];
}

-(void) videoFivePreloadDidFinish:(id)sender {
	NSLog(@"preload");
}

-(void) videoSixPlayBackDidFinish:(NSNotification*)aNotification {
	NSLog(@"playback stop");
	moviePlayerObject = [aNotification object];
	
	
    [[NSNotificationCenter defaultCenter] removeObserver:self
	 
													name:MPMoviePlayerPlaybackDidFinishNotification
	 
												  object:moviePlayerObject];
	
    // Release the movie instance created in playMovieAtURL:
	
    [moviePlayerObject release];
}

-(void) videoSixPreloadDidFinish:(id)sender {
	NSLog(@"preload");
}
MattTrout is offline   Reply With Quote
Old 06-04-2009, 02:28 PM   #2 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

im assuming that the problem is that your video files are huge sizes... remember that the iphone screen is 3 inches by 2 inches. make your video sizes way smaller thus decresing the file sizes but keeping the same quality if not better when resizing to an iphone screen
GOOD LUCK!
orange gold is offline   Reply With Quote
Old 06-04-2009, 02:46 PM   #3 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 900
jsd is on a distinguished road
Default

OK, first of all, do you know about arrays or variables or anything to avoid having to write the exact same code six times like that? It hurts my head.

You don't need the releasepools at all - you are using all foundation class convenience methods so you don't have to worry about releasing anything.

You've got a bug in videothree - the variable you construct is videoThreecallmoviePath but you return something else - that shouldn't even compile, but maybe it just gives a warning. If it does, that probably would cause a crash.

I would try running with NSZombiesEnabled = YES to see if you're trying to access a prematurely released object. Oh and figure out how to abstract things so you don't need six separate copies of every method... that's just crazy.
jsd is offline   Reply With Quote
Old 06-04-2009, 03:18 PM   #4 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 23
MattTrout is on a distinguished road
Default

orange gold - Is there like an ideal limit to the filesize for playing a local video? I converted the video files using itunes for ipod and iphone(they are 320x480).

jsd - i'll try using NSZombiesEnabled...and I know that I should use an array, i'm kind of just learning it all so just wanted to see if I could get the idea to work but thanks for the heads up.
MattTrout is offline   Reply With Quote
Old 07-24-2009, 01:25 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 3
unrealreality is on a distinguished road
Default

Hello, I'm just starting out building iPhone apps. I, too, needed to play more than one video in my project. I tried creating a seperate class to load movies when I needed them, but it's not working perfectly, and in the interest of time I found your code and used that. Nice and streamlined. I see what you mean by the crashing after 2 plays. It happens to me too. I fixed it though. when you set up the notification part (which i still don't quite understand, but in time), you have this:

.....name:MPMoviePlayerPlaybackDidFinishNotificati on object:nil];

you have to set nil to the object you're checking. in your case it's
......object:moviePlayerObject];

I did that and now my videos play as many times as I click on them and hit done. Hope it works for you if you haven't already figured it out.

cheers.
unrealreality is offline   Reply With Quote
Reply

Bookmarks

Tags
memory leaks, memory management, playback, video

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 337
6 members and 331 guests
givensur, ipodphone, jbro, mer10, mtl_tech_guy, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:10 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0