我正在开发一个非常重的仅 iPad 的 iOS 应用程序,该应用程序使用 ARC,但是当我尝试使用 MPMoviePlayerController 时似乎出现了泄漏,仪器在为视频播放器对象分配内存的代码行上引发了内存泄漏,任何想法? 当视频完成播放时,视频播放器的清理似乎也没有发生。
任何帮助将不胜感激,到处寻找答案,因为您可以说这个问题在很大程度上是应用程序性质的阻碍。
编码:
@interface ViewController ()
@property(nonatomic,strong) MPMoviePlayerController * vidPlayer;
@end
@implementation ViewController
@synthesize vidPlayer;
- (void)viewDidLoad
{
@autoreleasepool {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self playVideoForFile:@"01_intro"];
}
}
-(void)playVideoForFile:(NSString*)p_fileName
{
NSString *path = [[NSBundle mainBundle] pathForResource:p_fileName ofType:@"mp4"];
NSURL *tempURI = [NSURL fileURLWithPath:path];
vidPlayer = [[MPMoviePlayerController alloc] initWithContentURL:tempURI];
[vidPlayer setControlStyle:MPMovieControlStyleNone];
[vidPlayer setAllowsAirPlay:NO];
[vidPlayer.view setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height,[[UIScreen mainScreen] bounds].size.width)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vidFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:vidPlayer];
[vidPlayer play];
[self.view addSubview:vidPlayer.view];
}
-(void)vidFinishedCallback:(NSNotification*)aNotification
{
[vidPlayer pause];
vidPlayer.initialPlaybackTime = -1;
[vidPlayer stop];
vidPlayer.initialPlaybackTime = -1;
[vidPlayer.view removeFromSuperview];
vidPlayer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:vidPlayer];
}