Assumed precondition: your project is using ARC.
Your MPMoviePlayerController
instance is local only and ARC has no way of telling that you need to retain that instance. As the controller is not retained by its view, the result is that your MPMoviePlayerController
instance will be released directly after the execution of your playMovie
method execution.
To fix that issue, simply add a property for the player instance to your SRViewController
class and assign the instance towards that property.
Header:
@instance SRViewController
[...]
@property (nonatomic,strong) MPMoviePlayerController *player;
[...]
@end
Implementation:
@implementation SRViewController
[...]
-(IBAction)playMovie
{
NSString *url = [[NSBundle mainBundle]
pathForResource:@"OntheTitle" ofType:@"mov"];
self.player = [[MPMoviePlayerController alloc]
initWithContentURL: [NSURL fileURLWithPath:url]];
// Play Partial Screen
self.player.view.frame = CGRectMake(10, 10, 720, 480);
[self.view addSubview:self.player.view];
// Play Movie
[self.player play];
}
[...]
@end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…