【发布时间】:2012-04-16 17:27:03
【问题描述】:
我正在开发一个非常重的仅 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];
}
【问题讨论】:
-
你能贴一张仪器结果的截图吗?
标签: ios mpmovieplayercontroller automatic-ref-counting